VBA:“参数不是可选的”,但我的函数没有参数?

时间:2014-02-05 18:15:26

标签: vba excel-vba excel

当我调用一个没有参数的函数时,我不确定为什么我收到错误,“Argument not optional”:

Sub CreateQACheck()
' ...do stuff
    Dim QAFileName As String
    QAFileName = FindQACheckFile
    '...
End Sub

Function FindQACheckFile() As String
    ' Do stuff...
    FindQACheckFile = something
End Function

我应该在那里放置未使用的参数吗?

编辑:我发现了问题(缺少括号),我将在下面发布答案,但这里是存在问题的代码:

Function FindQACheckFile() As String
    Dim QAFileDir As String, QAFileName As String, QAFilePath As String
    QAFileDir = "c:\something"
    If Len(Dir(QAFileDir)) = 0 Then 
        QAFileDir = "c:\somethingelse"
    End If
    QAFileName = "qacheckfile.xlsm"
    QAFilePath = QAFileDir + QAFileName
    If Len(Dir(QAFilePath)) = 0 Then ' If it's not there, gotta ask the user where it is
        MsgBox ("I can't find the QA check file! Please let " + MGlobal.DEVNAME + " (" + MGlobal.DEVEMAIL + ") know, but for now point me in the right direction...")
        Dim fd As FileDialog
        Set fd = Application.FileDialog(msoFileDialogOpen)
        With fd
            .AllowMultiSelect = False
            If .Show = -1 Then
                QAFilePath = .SelectedItems
            End If
        End With
    End If
    FindQACheckFile = QAFilePath
End Function

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

QAFilePath = .SelectedItems

我忘记了这是一个数组,所以看起来应该是这样的:

QAFilePath = .SelectedItems(1)

我不确定为什么它在功能名称上出错而不是在那条线上特别错误。