Because VBA is a programming language, it shares common principles and elements with other languages. One of them is variables.
A variable is a reserved place in a computer memory that can be used by a program. Excel stores there the value assigned to that variable.
Declaring Variables
When you declare the variable, you can define what kind of data you want to store there. For example:
With data type
1 |
Dim myVal As Integer |
It means that you reserve 2 bytes for the integer value.
Without data type
If you declare value without data type
1 |
Dim myVal |
VBA will automatically create its default type called Variant. This data type can be very handy because it changes its type depending on what you actually do with the variable. For example, if you enter „56”, it can behave as a number or as a text string.
Declaring multiple variables at once
When you want to declare multiple variables, you can create each declaration in a new line.
1 2 3 |
Dim first As Integer Dim second As Double Dim third As Single |
But there is another faster method where you need only one line of code.
1 |
Dim first As Integer, second As Double, third As Single |
If all of your variables are of the same type, you still have to declare each variable separately.