VBA - Namespace

Namespace is how a program group variables together and how you can access them at certain point in the execution. Understanding how VBA does this will help you avoid namespace errors.

VBA namespace allows reuse of names.

Here is a code example showing a variable, x, is assigned a value 12.

It then used in a function which assigns the value 113.

It would be easy to assume that the msgbox would contain 133, but it returns 12. This is due to x have two values which depend on where in the code you are. Inside the Sub x is 12, in the function x is 113.

Code Snippet:

Sub a_namespace()

Dim x As Integer

x = 12

func1 (x)

MsgBox x

End Sub

Function func1(x As Integer)

x = 113

End Function

Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.

Must Read Articles

VBA and Microsoft Excel - Getting the most out of excel with VBA programming
VBA and Microsoft Word - Getting the most out of word with VBA programming
Application Object - Using the applications features in your code.
Installing VBA and First Program - What do you need to start using visual basic application VBA.