560 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
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.
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 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:
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 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.
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.
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.
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.