CongdonJL
CongdonJL

Reputation: 139

Sub getting "Expected: =" after adding variable

I am writing a sub to do some stuff and it is working great with 1 variable. When I try to add a second it throws an error saying that it is expecting an =.

Works Fine:

Call Sub

makeUniqueID (compare2)

Create Sub

Sub makeUniqueID(sheet As String)
 'does some stuff
End Sub

Throws Error

Call Sub

makeUniqueID(compare2 , compare1)

Create Sub

Sub makeUniqueID(sheet As String, title As String)
  'Does some Stuff
End Sub

Upvotes: 0

Views: 51

Answers (3)

chris neilsen
chris neilsen

Reputation: 53126

Adding to @Jean 's answer:

Calling a Sub without Call and enclosing a parameter in () actually changes the behaviour of the Sub

By default Parameters are called ByRef, so

Sub makeUniqueID(sheet As String)

Is the same as

Sub makeUniqueID(ByRef sheet As String)

Calling a Sub it like this:

makeUniqueID (compare2)

overrides the reference type, and makes it a ByVal call.

The bottom line is saying makeUniqueID (compare2) "Works Fine" is not quite true.

This only matters when you intend the Sub to modify the source parameter passed to it.

Upvotes: 0

Get rid of the parentheses when calling a Sub:

makeUniqueID compare2, compare1

Alternatively, as @niallmcfc points out, you can call a sub like this:

Call makeUniqueID(compare2, compare1)

But this Call notation adds unnecessary clutter and has fallen out of favour. Documentation on MSDN.

You only really need parentheses when calling a function, e.g.:

result = myFun(var1, var2)

Upvotes: 2

Overt_Agent
Overt_Agent

Reputation: 519

Try putting "Call" in front of the sub when you call it-

Call makeUniqueID(compare2, compare1)

Upvotes: 1

Related Questions