Please note javascript is required for full website functionality.

Blog

VBA Blogs: Going Through the Visual Basics – Part 7

21 September 2018

We thought we’d run an elementary series going through the rudiments of Visual Basic for Applications (VBA) as a springboard for newer users.  This blog looks at declaring variables.

 

To maintain good coding practice to declare variables, it is easy to ask VBA to force the coder to declare them all the time. This is done using the opening statement Option Explicit.

When Option Explicit appears in a file, you must explicitly declare all variables by using the Dim or ReDim statements.  If you try to use an undeclared variable name, an error occurs at compile time.

As an example, let’s start with the following code:

Option Explicit

Sub OETest()

    myTestString = "Hello World!"

    MsgBox myTestString

End Sub

Running this code results in the following:

The compiler stops with an error alert and snaps back to the Visual Basic Editor to the variable that has not been declared. The procedure must be stopped and the declaration statements added before it will successfully execute.

Newsletter