【问题标题】:Compile Error - Argument Not Optional编译错误 - 参数不是可选的
【发布时间】:2018-03-06 14:27:57
【问题描述】:

运行指向行的 vba 代码时出现编译错误:参数不是可选的错误。 MsgBox (RemoveFirstChar)

代码:

Sub test()
Dim Currworkbook As Workbook
Dim CurrWKSHT As Worksheet
Dim Filename As String
Dim BCName As String
Dim Str As String

FFolder = "C:\user"
CurrLoc = "File3"

If CurrrLoc = "File3" Then
    CurrLoc = FFolder & "\" & CurrLoc
    Set FSobj = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set FFolderObj = FSobj.GetFolder(CurrLoc)
    If Err.Number > 0 Then
      '
    End If
    For Each BCObj In FFolderObj.Files
        'BCName = Right(BCObj.Name, (Len(BCObj.Name) - InStrRev(BCObj.Name, "\", 1)))
        If IsNumeric(Left(BCObj.Name, 4)) <> True Then
            Call RemoveFirstChar(BCObj.Name)
            'Str = RemoveFirstChar
            MsgBox (RemoveFirstChar)  '--->Error: Compile Error: Argument Not Optional 
        Else
            MsgBox (BCObj.Name)
        End If
    Next

End If



End Sub

Public Function RemoveFirstChar(RemFstChar As String) As String
Dim TempString As String
TempString = RemFstChar
If Left(RemFstChar, 1) = "1" Then
    If Len(RemFstChar) > 1 Then
        TempString = Right(RemFstChar, Len(RemFstChar) - 1)
    End If
End If
RemoveFirstChar = TempString
End Function

【问题讨论】:

  • 删除():MsgBox RemoveFirstChar
  • 并删除上面的调用行并将msgbox更改为MsgBox RemoveFirstChar(BCObj.Name)
  • 您还需要删除下面的MsgBox()MsgBox BCObj.Name

标签: vba excel compiler-errors compilation


【解决方案1】:

RemoveFirstChar 是一个用户定义的函数,需要一个非可选字符串作为参数。

Public Function RemoveFirstChar(RemFstChar As String) As String
     ....
End Function

我想你想摆脱 Call RemoveFirstChar(BCObj.Name) 然后使用,

MsgBox RemoveFirstChar(BCObj.Name)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多