Variables and Constants in Excel Visual Basic
Part three 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 (this blog)
  4. Using Variables (Assigning and Retrieving Values)
  5. Forcing Declaration of Variables (Option Explicit)
  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.

Declaring Variables

The main way to create a variable is with a DIM statement (you can also use Private, Public and Global, as we'll see when we get on to talk about scope).

The syntax is:

Dim VariableName As VariableType

In this command:

  • VariableName can be any valid name for your variable
  • VariableType can be any valid type of data

You can also declare multiple variables on one line:

'the name of each superhero and their rating

Dim HeroName As String, HeroRating As Long

Be careful with this method though - the following code will create HeroName1 to be of type Variant, since you haven't specifically given it a type:

'be sure to give variables specific types when using multiple declaration

Dim HeroName1, HeroName2 As String

Variable Names

Variable names can be pretty much anything you like (although they can't contain spaces and certain other punctuation characters, and can't begin with a number).  Here are some good names:

Variable name Why it's good
SuperheroName Not only does it describe exactly what sort of data it will contain, but it's unlikely that it will conflict with an internal reserved VBA word.
HeroRating Again, this is descriptive, and probably won't conflict with any internal VBA word.

Here are some bad names, showing traps to avoid:

Variable name Why it's bad
Name You're playing with fire with this name.  It's highly likely that Microsoft have used this word for their own internal purposes in VBA, and this can lead to bugs which are almost impossible to track down! Avoid using single words which could possibly be misconstrued by Excel.
n This has the merit of being short, but it's hard to guess what it means.  This will make your code impossible to read if you're using more than 2 or 3 variables (as you will be).
superheroname Nothing wrong with this, except that because it doesn't use CamelCase (hope it's obvious where the name comes from) it's hard to read.  Is this SuperHeroName?  Or SuperHeronAME?
Super_Hero_Name Using underscores as separators is just SO last year!

If you use compound words and camel case, you can't go far wrong.

Variable Types

This isn't a reference website, so let's keep things simple.  You can survive and thrive with just 6 variable types:

Type Notes Example
String Holds any string of characters or text Batman
Long Holds any whole number 42
Double Holds any floating point number 3.14
Boolean Can only hold the values True and False True
Date Holds any date and time combined 25/12/2025 12:45:00
Variant Use when you're not sure of the data type. Variants don't care WHAT you put into them! Anything!

The above data types may waste a couple of bytes of storage data, but with modern computers you won't notice the difference. 

The Variables for our Example

Putting all of the above together, we get:

Sub RecordVote()

'the name of each superhero

Dim HeroName As String

'the rating assigned to them

Dim HeroRating As Long

It's traditional to declare all of the variables that you're going to use at the top of each procedure, or program, although there's nothing in VBA saying that you have to do this.

Now that we know how to create variables, it's time to assign values to them.

This blog has 0 threads Add post