Optional argument in VBA causes error while executing

756 views Asked by At

I tried to create procedure that passes 2 arguments, mandatory and optional, before I added optional argument procedure was running correctly. Here is Code:

Sub a2(var As String, Optional num As Integer = 5)
MsgBox (num)
End Sub

Sub start_a2()
a2 ("null_text", 5)
End Sub

When I pass any second argument, running procedure start_a2 fails at 1st line: Sub start_a2(), VBA higlight this line with Yellow and returns Syntax error, but do not provide any details. Second argument is inproperely passed?

1

There are 1 answers

5
Arin On BEST ANSWER

Does it work if you use Call? Such as

Sub start_a2()
   Call a2("null_text", 5)
End Sub

Edit: Though the above will work, @SO's comment below is right on (Thanks!); you can just use

Sub start_a2()
   a2 "null_text", 5
End Sub