Skip to main content

Using parentheses in code

Sub procedures, built-in statements, and some methods don't return a value, so the arguments aren't enclosed in parentheses. For example:

MySub "stringArgument", integerArgument

Function procedures, built-in functions, and some methods do return a value, but you can ignore it. If you ignore the return value, don't include parentheses. Call the function just as you would call a Sub procedure. Omit the parentheses, list any arguments, and don't assign the function to a variable. For example:

MsgBox "Task Completed!", 0, "Task Box"

To use the return value of a function, enclose the arguments in parentheses, as shown in the following example.

Answer3 = MsgBox("Are you happy with your salary?", 4, "Question 3")

A statement in a Sub or Function procedure can pass values to a called procedure by using named arguments. The guidelines for using parentheses apply, whether or not you use named arguments. When you use named arguments, you can list them in any order, and you can omit optional arguments. Named arguments are always followed by a colon and an equal sign (:=), and then the argument value.

The following example calls the MsgBox function by using named arguments, but it ignores the return value.

answer3 = MsgBox(Title:="Question 3", Prompt:="Are you happy with your salary?", Buttons:=4)

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>