1

I am trying to pass more than one parameter in VBA for Excel but everytime I do I get a:

Compile error:

Syntax error

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 Answers 2

3

You can Call first(2, 3) or you can first 2, 3 or even first fOne:=2, fTwo:=3 but you cannot 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

There is no problem passing multiple parameters to a sub if the syntax is correct.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this is great, as a quick follow-up is one of these a "preferred method" or are they all about equal?
Some coders don't like to use Call; they say it is obsolete. Doesn't make much difference to me but some say I'm obsolete too. A lot of code I provide here uses named parameters (third example) but that is for instructional purposes as much as anything else. Named parameters can help identify a single optional parameter out of order. It really boils down to personal preference but pick one and stick with it.
-1

You pass arguments to Sub, but you can pass arguments to Function only:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.