MOCKSTACKS
EN
Questions And Answers

More Tutorials









VBA Procedure Calls

This is confusing. Why not just always use parentheses?


Parentheses are used to enclose the arguments of function calls. Using them for procedure calls can cause
unexpected problems.

Because they can introduce bugs, both at run-time by passing a possibly unintended value to the procedure, and at
compile-time by simply being invalid syntax.

Run-time


Redundant parentheses can introduce bugs. Given a procedure that takes an object reference as a parameter...

Sub DoSomething(ByRef target As Range)
End Sub

...and called with parentheses:

DoSomething (Application.ActiveCell) 'raises an error at runtime

This will raise an "Object Required" runtime error #424. Other errors are possible in other circumstances: here the Application.ActiveCell Range object reference is being evaluated and passed by value regardless of the procedure's signature specifying that target would be passed ByRef. The actual value passed ByVal to DoSomething in the above snippet, is Application.ActiveCell.Value.

Parentheses force VBA to evaluate the value of the bracketed expression, and pass the result ByVal to the called procedure. When the type of the evaluated result mismatches the procedure's expected type and cannot be implicitly converted, a runtime error is raised.

Compile-time


This code will fail to compile:

MsgBox ("Invalid Code!", vbCritical)

Because the expression ("Invalid Code!", vbCritical) cannot be evaluated to a value.

This would compile and work:

MsgBox ("Invalid Code!"), (vbCritical)

But would definitely look silly. Avoid redundant parentheses.

Conclusion

In this page (written and validated by ) you learned about VBA Procedure Calls . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Conditional Compilation.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.