What is a variable?
Think of it this way, a variable is like a little box in your computer’s memory. The little box, the variable, will have a name. You store things in variables, like Dates, Strings, or Objects. At some point, you will retrieve the contents of the variable and do something with it. For example, you might display the Date that is being stored. You retrieve the contents of the variable by calling it by its name. You can name your variables pretty much whatever you want, but there are some rules and conventions to variable naming in VBA.
VBA Variable Rules
These are musts. All variables must follow these rules:
- The name must begin with a letter.
- The name must contain only letters, numbers, and underscores (no other symbols).
- The name cannot be more than 255 characters long.
- The name cannot be a reserved word like Print or Save.
VBA Variable Conventions
These are conventions. All variables should follow these conventions.
- Always use a three-letter prefix that describes the type of data the variable will hold.
- Capitalize the first letter of every actual word (but not the prefix) in the variable. This is called lower camel case. For example:
- strFirstName
- dtmBirthDate
- rptMonthlyReport
So, how do you know if a word is reserved? Well, by putting the three-letter prefix in front of the variable, you ensure that it will not be a reserved word. For example, Print is a reserved word, but strPrint is not.
Do you have to declare your variables? No. Should you declare your variables? YES! Read more about declaring variables and using Option Explicit in VBA.
To hear about the latest Office 2010 news, blogs, and training, subscribe to our newsletter. Click here to subscribe.





Leave a Reply