Declaring variables using the various data types in VBA
This guide breaks down VBA variables, Data Types, and Dim. Typically, the very first step after naming your macro is declaring your variables. Variables are names for different pieces of the data that the macro will be working with.
However, this sometimes proves difficult since it’s hard to plan ahead how many variables will be used in the macro. Eventually, when the macro is written, the user may add or remove certain variables. This will become more apparent further into this guide to writing VBA macros.
The very top of each macro after the sub name is a section called the declarations. Here, the user lists and names all the different variables he or she will use, and declares their data types. This is done by using the “Dim” statement. The “Dim” statement is followed by the name of the variable, and sometimes the statement “as [datatype]”.
For example, if we wanted to create a variable for a Stock Price, we could write “Dim stockPrice as double”. This creates a variable called the stockPrice, which takes on the data type double. A double data type is one of the data types that allows for decimals, as opposed to the integer data type.
It’s not necessary to always declare the data type. Sometimes, it’s sufficient to declare the name, and VBA can infer the data type when the variable is used in the code later on. However, it’s generally safer to declare the data type you expect to use.
Each declaration will take its own line. It’s helpful to group variables of the same data type together.
There are quite a few VBA data types, but for the general purposes of financial modeling not all of them are used.
Below is a list of common VBA variables (known as data types) used in macros and their purposes:
Again, there are other data types, but these are the most commonly used for creating macros.
After a variable has been created, storing a value in it is simple.
Variable name = Variable value
String variable name = “Variable value”
(When using strings, you have to surround the text in quotation marks. This is not true for number or binary values)
Each named variable can only hold one value at a time.
Here is a break down of how to use Dim in VBA:
As you can see in the above example, these variables (and some extra variables to show grouping best practices) have been declared. Values have also been stored in the main variables.
However, if this macro were to be run, it would simply store these values in the variables, and not use them in any way. To continue learning how to use variables, you need to know the VBA methods available to each one.
Thank you for reading CFI’s guide to VBA variables, Data Types, and Dim. To keep learning and progressing your Excel skills we highly recommend these additional CFI resources:
To master the art of Excel, check out CFI’s Excel Crash Course, which teaches you how to become an Excel power user. Learn the most important formulas, functions, and shortcuts to become confident in your financial analysis.
Launch CFI’s Excel Crash Course now to take your career to the next level and move up the ladder!