1

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?

user3376246
  • 413
  • 3
  • 9
  • 17
  • Already answered here: http://stackoverflow.com/questions/479891/what-does-the-call-keyword-do-in-vb6 – SierraOscar Jun 19 '15 at 21:16
  • Add `ByVal` before both variables and let me know about result... – Maciej Los Jun 19 '15 at 22:02
  • No one seems to have addressed why your code worked prior to adding the the second argument. Presumably it was ``a2 ("null_text)``. The reason that that worked is that placing parenthesis around argument will cause the argument to be passed ByVal vs ByRef which is the default mode. Therefore, ``a2 ("null_text)`` is not the same as ``Call a2("null_text)``. – TnTinMn Jun 20 '15 at 04:21

1 Answers1

2

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
Arin
  • 1,373
  • 2
  • 10
  • 23
  • 1
    The `Call` keyword is a throwback from legacy VBA. Unless you are assigning a value back to/from something you don't need to include parentheses. Try using `a2 "null_text", 5` instead of `a2("null_text", 5)`. You will notice that as soon as you press the spacebar after typing "a2" the IntelliSense will kick in and it will prompt you for the first argument. – SierraOscar Jun 19 '15 at 21:15
  • 1
    @ S O, good advice. I always saw `a2 "null_text", 5` and `Call a2 ("null_text", 5)` as equivalents. I have no love for the `Call` keyword, but the parentheses seem more consistent with other languages. – Arin Jun 19 '15 at 21:22
  • It's true that parentheses are a "staple" part of most other OOP languages, and VBA being derived from VB did originally follow the conventional method of `Call`ing another procedure, a lot of keywords and syntax in VBA are no longer required but have to be left in for backwards compatibility. Purely a matter of preference and what you are used to. – SierraOscar Jun 19 '15 at 21:26
  • @A.Franklin Tank You very much, both commads work, I think that Call and brackets has better visibility. – user3376246 Jun 20 '15 at 08:50
  • That's a pitty the brackets without the `Call` does not work! – lalebarde Oct 21 '21 at 16:54