WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
30 years in business
Wise Owl Training
30 years in business
See 519 reviews for our classroom and online training
Variables and Constants in Excel Visual Basic
Part five of an eight-part series of blogs

This blog explains the nuts and bolts of Excel VBA variables, including how to declare them, how to set their scope and all sorts of other tips and tricks. The blog also covers constants in Excel macros.

  1. Variables in Excel Visual Basic macros
  2. What is a Variable? VBA macro variables.
  3. Declaring Variables in Visual Basic for Applications
  4. Using Variables (Assigning and Retrieving Values)
  5. Forcing Declaration of Variables (Option Explicit) (this blog)
  6. Useful Things to Know about Variables
  7. The Scope of Variables
  8. Constants in Excel Visual Basic for Applications

This blog is part of our Excel macros online tutorial series.  Alternatively, we run training courses in the UK in Excel and also in Visual Basic for Applications (and are always looking for partners in the US or other English-language countries!).

Posted by Andy Brown on 05 September 2011

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Forcing Declaration of Variables (Option Explicit)

Why you should Declare All Variables

The example on the previous page would run just as well if you didn't declare your variables:

Example with varaibles commented out

If you comment out the variable declarations like this, your macro will still work.


In the above case, VBA will reach the first two lines creating variables:

HeroName = Range("C4")

HeroRating = Range("C6")

Because you haven't explicitly declared HeroName and HeroRating, VBA will create them on the fly for you, giving each the type Variant.    So far, so good?

Well, yes - until you mistype a variable name.  Here's what would happen for a simple mistyping:

 Misprinted VBA code Superhero wrongly entered
There's a misprint here - how quickly can you see it? The superhero you get as a result

The problem above could be avoided so easily, if you could just tell VBA that wherever you use a variable, you have to declare it.  Read on!

Forcing Declaration of Variables

The solution to the above problem is to include two magic words at the top of each module - Option Explicit:

Option Explicit

Sub RecordVote()

'the name of each superhero and their rating

'Dim HeroName As String

'the rating assigned to them

'Dim HeroRating As Long

'go to the votes sheet and get the value of the superhero, and their rating


HeroNane = Range("C4").Value

HeroRating = Range("C6").Value

Here's what you get if you run the above macro now:

Example of variable not defined message

The result of running the macro.  VBA highlights the variable you've misspelt, and tells you that it's not defined. 


Setting the Option to Require Variables as a Default

Given that forcing yourself to declare all variables like this is such a good idea, how can you avoid having to type in Option Explicit at the top of every new module you create?  Here's how!

  1. From the menu within VBA (not Excel) select Tools -> Options.
  2. Tick the Require Variable Declaration box shown below.
  3. Select OK.
Require variables tick box

Tick the box shown!

From now on, every time you create a new module it will have the words Option Explicit at the top, and you'll have to declare any variables that you use.


This blog has 0 threads Add post