VIsual基本应用程序传递多个参数

时间:2016-02-12 09:38:50

标签: excel vba

我试图在VBA for Excel中传递多个参数但每次我都会得到一个:

  

编译错误:

     
    

语法错误

  
Sub first(ByVal fOne As Integer, ByVal fTwo As Integer)

    If fOne = 2 Then
        MsgBox "fTwo"
    End If

End Sub

Sub second()
    first(2, 3)

End Sub

2 个答案:

答案 0 :(得分:3)

您可Call first(2, 3)first 2, 3甚至first fOne:=2, fTwo:=3,但不能first(2, 3)

Sub first(ByVal fOne As Integer, ByVal fTwo As Integer)

    If fOne = 2 Then
        MsgBox "fTwo"
    End If

End Sub

Sub second()
    first 2, 3
    'alternate
    Call first(2, 3)
    'alternate
    first fOne:=2, fTwo:=3
End Sub

如果语法正确,将多个参数传递给sub是没有问题的。

答案 1 :(得分:-1)

您将参数传递给Sub,但您可以将参数传递给仅函数:

Function first(ByVal fOne As Integer, ByVal fTwo As Integer)

If fOne = 2 Then
    MsgBox "fTwo"
End If

End Function

Sub second()
    Call first(2, 3)

End Sub