excel+;vba和x2B;编译错误AutoOpenRequiredWorkbook(myFileNameToOpen,myFilePath)可以';我不明白为什么我';我得到了这个错误

excel+;vba和x2B;编译错误AutoOpenRequiredWorkbook(myFileNameToOpen,myFilePath)可以';我不明白为什么我';我得到了这个错误,excel,vba,function,Excel,Vba,Function,当我运行以下代码行时,我得到编译错误Expected:=: MiscFunctions.AutoOpenRequiredWorkbook(myFileNameToOpen,myFilePath) 下面是返回错误的函数的代码: Function AutoOpenRequiredWorkbook(myFileNameToOpen As String, myFilePath As String) As String Dim OpenedOk, FileToOpen As String Opened

当我运行以下代码行时,我得到编译错误
Expected:=

MiscFunctions.AutoOpenRequiredWorkbook(myFileNameToOpen,myFilePath)

下面是返回错误的函数的代码:

Function AutoOpenRequiredWorkbook(myFileNameToOpen As String, myFilePath As String) As String
 Dim OpenedOk, FileToOpen As String
 OpenedOk = "NOT Opened"
    If UserName = "scorekeeper" Then GoTo NothingElseTodoForscorekeeper: ' NothingElseTodoForscorekeeper
        'TempPath = Environ("userprofile")
        FileToOpen = [myFilePath] & myFileNameToOpen '' FileToOpen = TempPath & "\OneDrive\MasterFiles\" & FileNameToOpen
        'Stop  ' just for debugging

        If IsFileOpen(myFileNameToOpen) Then
        OpenedOk = "OpenedOk"
            GoTo AlreadyOpen
         Else
            Workbooks.Open filename:=myFileNameToOpen, UpdateLinks:=0
            Windows(myFileNameToOpen).Visible = False    ' hide this workbook
            OpenedOk = "OpenedOk"
        End If
NothingElseTodoForscorekeeper:
AlreadyOpen:
AutoOpenRequiredWookbook = OpenedOk
'This example names window one in the active workbook Consolidated Balance Sheet. _
'This name is then used as the index to the Windows collection.
'ActiveWorkbook.Windows(1).Caption = "Consolidated Balance Sheet"
'ActiveWorkbook.Windows("Consolidated Balance Sheet").ActiveSheet.Calculate

End Function
使用以下命令:

MiscFunctions.AutoOpenRequiredWorkbook myFileNameToOpen, myFilePath
当您将函数返回的值赋给变量时,需要在函数的参数周围使用括号。i、 e:

x = MiscFunctions.AutoOpenRequiredWorkbook(myFileNameToOpen, myFilePath)

这也行。

多亏了DecimalTurn,我加入了byref部分,解决了这个问题。 函数AutoOpenRequiredWorkbook(ByVal myFileNameToOpen为字符串,ByVal myFilePath为字符串)为字符串
非常感谢你抽出时间。我希望有一天我能报答你的好意。

它无法编译的原因是VBA试图计算表达式
(myFileNameToOpen,myFilePath)
,并将其结果作为调用方法的第一个参数传递。删除括号会使参数列表被解析成这样,而不是作为要计算的表达式。我肯定是出了什么问题。首先,非常感谢你抽出时间来帮助我`MiscFunctions.AutoOpenRequiredWorkbook myFileNameToOpen,myFilePath'@alanBumgarner您是否收到与以前相同的错误?将代码更改为MiscFunctions.AutoOpenRequiredWorkbook myFileNameToOpen后,否,myFilePath“我现在遇到的错误是byref参数类型不匹配”我打赌您已经检测到我是堆栈溢出新手,因为我不确定是否或如何最好地使用此网站提供和获取信息。谢谢你的耐心和帮助