0

I have a small VBA macro in Word that will not compiles with an Expected: = error. Since these are all subroutines I am not sure what would cause this. I have simplified this as much as possible to the code listed below.

Sub test()
    tmp = MsgBox("test")
    test2("tmp2","tmp3") ' this is the line where the compile error appears.

End Sub


Sub test2(test1String As String, test2String As String)
    MsgBox (test1String)
End Sub

I would not expect there to be any assignment errors with something this simple. Enter the code and try to run the test macro.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
cebess
  • 11
  • 4

1 Answers1

0

You are calling a Sub, not a Function. The syntax

test2("tmp2","tmp3") 

calls the sub test2 with only one argument, namely an (invalid) expression ("tmp2","tmp3")

You should call it as

test2 "tmp2","tmp3"

The braces ( and ) turns the argument list in an expression.

Note: also when calling a function with braces around the argument list requires that the function result is assigned, otherwise the function arguments too are evaluated as an expresion

i = myFunction(1, 2)     ' valid
myFunction 1, 2          ' valid
myFunction(1, 2)         ' invalid
mySub(1, 2)              ' invalid
mySub 1, 2               ' valid
mySub(1 + 2), (3 + 4)    ' valid!

The last example calls the sub with two arguments, which both are expressions.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41