VBA return 上的布尔函数类型不匹配

VBA Boolean Function type mismatch on return

我在这里搜索了一个上午,但没有发现与此特定错误类似的内容。在调试 Excel VBA 宏时,我遇到了一个非常令人沮丧的 运行 时间错误 - 在分配函数 return 值时出现类型不匹配 13 错误 'Display_Form' 到布尔变量 'success'。窗体显示和行为正常,但在退出窗体然后调用函数时,会弹出此错误。这是代码。我正在用这个撞墙。有什么想法吗?

Public Sub Add_New_Account()
    ' Process userforms to create new Bank account

    Dim success As Boolean
    
    success = Display_Form(NewAccountForm1)
    If success Then
        Select Case accountDict(ACCT_TYPE_HEADER)
            Case CREDIT_CD, CREDIT_IFL, CREDIT_DEPT
            Case BANK_CHK, BANK_SAV
                ' Nothing needed at this time
        End Select
    End If
    
End Sub
Public Function Display_Form(targetForm As Object, Optional leftPos As Integer = 0, Optional topPos As Integer = 0) As Boolean
    ' Open the 'targetForm' userform
    Display_Form = True
    On Error GoTo ErrorHandler
    Application.EnableCancelKey = xlErrorHandler

    With ActiveSheet
        Load targetForm
        With targetForm
          .StartUpPosition = 0
          If leftPos Then
            .Left = leftPos
          Else
            .Left = Application.Left + Application.Width - .Width
          End If
          If topPos Then
            .Top = topPos
          Else
            .Top = Application.Top + Application.Height - .Height
          End If
          .Show vbModal
        End With
        Unload targetForm
    End With
    Exit Function
    
ErrorHandler:
    ' Handle Ctrl-Break gracefully
    If Err.Number = 18 Then
        MsgBox "Use the Cancel button instead of Ctrl-Break"
        Resume
    Else
        ' Some other error occurred
        MsgBox "Sorry, something went wrong: " & vbNewLine & _
             Err.Number & " - " & Err.Description & vbNewLine & _
             "Macro ended unexpectedly.", vbExclamation
        Display_Form = False
    End If
End Function

我知道这是一种解决方法而不是一种解释,似乎对我有用

Dim success As Boolean, oForm as object

set oForm = NewAccountForm1
success = Display_Form(oForm)

可能会提示其他人知道原因