Phone (01457) 858877 or email
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.
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!).
The example on the previous page would run just as well if you didn't declare your variables:

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:
![]() |
|
| 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!
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
Worksheets("Votes").Select
HeroNane = Range("C4").Value
HeroRating = Range("C6").Value
Here's what you get if you run the above macro now:

The result of running the macro. VBA highlights the variable you've misspelt, and tells you that it's not defined.
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!

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 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.
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!).
Comments on this blog
This blog has 4 comments:
Hi! I found extremely usefull this blog and the explanations are very clear. So far i have follow all the examples in the line of learning to use Macros in Excel 2010 but i found in a dead end: in the full code found in the post "Using Variables(Assigning and Retrieving Values", the line that says : "ActiveCell.Offset(0,1).Value= HeroRating" gives me an error when i execute the marco, recording the name and rating of the hero i wrote in the last line of my sheet, that is row #1048576. Please give me some help about this because i need learn basic VBA in Excel for my University Tesis.
Thank“s a lot for this priceless blog.
Hi
Thanks for the kind comments. I'm afraid I can't go through specific macros, but I can give you a couple of hints.
Firstly, when running your macros press F8 to step through them line by line, and after running each line (they appear in yellow) check back in Excel that the active cell is where you think it should be.
Secondly, you've ended up on the last row of the spreadsheet. The usual way to get there is with a command like:
ActiveCell.End(xlDown).Select
If you execute this at the bottom of a column of cells, you'll end up at the bottom of a worksheet.