The same with other programming languages, variables are vital in VBA.
If you’re not familiar with variables, here’s a good definition:
A variable stores a value. It stands in the place of a value.
There are 3 things you can do with a variable:
- Declare or create a variable
- Store a value in a variable
- Read the value in the variable
Also, there are lots of different types of variables. The most common are:
- String – for text
- Long – for integers
- Double – for decimals
- Date – for date and time
- Currency – similar to decimal but only up to 4 decimal places
- Boolean – for true or false
When creating a variable, you’ll have to use “Dim” which is short for dimension. It tells Excel that the next word is the variable name.
Then, you’ll have to use “As” as the declaration statement. It tells Excel that you’re going to use that data type for that variable.
As an example, let’s say you would create a variable called “company” which is a string of text.
Then, to set the variable, you’ll need to create a line that assigns a value.
In VBA, all you have to do is append “.Value” to the variable and put an equals (=) sign pointing to that value.
Here’s how it would look like in your VBA editor: