Parentheses and Argument Lists

As you have seen, the use of parentheses around arguments when calling procedures is a bit of a minefield, so the following sections summarize when to use them, at the risk of opening a can of worms and getting lost in mixed metaphors. Bear in mind that the same rules apply to argument lists of methods.

Without the Call Statement

Only place parentheses around the arguments when you are calling a function procedure and are also making use of the return value from the function procedure:

SalesData = GetInput("Enter Sales Data")

Don't place parentheses around the arguments when you are calling a function procedure and are not making use of the return value from the function procedure:

Getlnput "Enter Sales Data"

Don't place parentheses around the arguments when you are calling a sub procedure:

Postlnput SalesData, "B3"

An Important Subtlety Regarding Parentheses

The following is correct syntax and leads to untold confusion:

MsgBox ("Insert Disk")

It is not what it appears and it is not a negation of the parentheses rules. VBA has inserted a space between MsgBox and the left parenthesis, which it does not insert in the following:

Response = MsgBox("Insert Disk")

The extra space indicates that the parentheses are around the argument, not around the argument list. If you pass two input parameters, the following is not valid syntax:

MsgBox ("Insert Disk", vbExclamation)

The following is valid syntax:

MsgBox ("Insert Disk"), (vbExclamation)

It is fine to place parentheses around individual arguments, but not around the argument list. However, you might not get the result you expect.

Apologies if you are bored, but this is important stuff. It is more important when you get to refer to objects in parameter lists. Placing an object reference in parentheses causes VBA to convert the object reference to the object's default property. For example, (Range("B1")) is converted to the value in the B1 cell and is not a reference to a Range object. The following is valid syntax to copy A1 to B1:

Range("A1").Copy Range("B1")

The following is valid syntax but causes a run-time error: Range("A1").Copy (Range("B1"))

With the Call Statement

If you use the Call statement, you must place parentheses around the arguments you pass to the called procedure:

Call PostInput(SalesData, "B3")

Because Call is of limited use, not being able to process a return value, and muddies the water with its own rules, it is preferable not to use it.

0 0

Responses

  • cedivar goold
    How to reference a parentheses in vba?
    8 years ago

Post a comment