【问题标题】:VBA: "Argument not optional", but my function has no parameters?VBA:“参数不是可选的”,但我的函数没有参数?
【发布时间】:2014-02-07 03:15:49
【问题描述】:

当我调用没有参数的函数时,我不确定为什么会收到错误“参数不是可选的”:

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

【问题讨论】:

  • 在整个项目中检查某种重复名称(可能在模块、类等中)。该特定代码没有任何问题,除非有其他干扰..
  • 您的代码在我看来是正确的。您确定您没有在其他地方收到此错误吗?还是您创建了多个同名函数?
  • 工具 - 选项 - 常规 - 错误捕获 - 中断所有错误。打开该选项。您可能在另一个隐藏的点上遇到了错误。
  • 不要说' ...do stuffFindQACheckFile = something 你能告诉我们确切的代码吗?

标签: vba excel


【解决方案1】:

问题出在这一行:

QAFilePath = .SelectedItems

我忘了这是一个数组,所以它应该是这样的:

QAFilePath = .SelectedItems(1)

不过,我不确定为什么它会在函数名称上出错,而​​不是在该行上出错。

【讨论】:

    猜你喜欢
    • 2014-06-27
    • 2019-01-09
    • 2015-09-28
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多