You can declare variable by using the keyword Dim.

Dim boolean__ As Boolean 
Dim byte_____ As Byte
Dim currency_ As Currency
Dim date_____ As Date
Dim double___ As Double
Dim integer__ As Integer
Dim long_____ As Long
Dim short____ As Single
Dim string___ As String
Dim variant__ As Variant

link -

Variable Names

The first character must always be alphabetic.
You cannot use spaces or periods.
Variable names can use alphabetical characters, number and some punctuation characters but not ( * . , # $ % & !).
The use of the underscore character is also encouraged.
The name must start with a letter and can have a maximum of 254 characters.
A variable must begin with a letter or an underscore
Variable names can be constructed from letters and numbers and the underscore character.

Not Case Sensitive

Variable names are not case sensitive and by default the interpreter adjusts the names of all variables with the same letters so that their case matches the case in the variable declaration (i.e. when using Dim).
If the variable was not declared using Dim then the case matches that of the most recently typed variable.


Once a variable has been declared it can be seen in the intellisense drop-down list.
Press (Ctrl + J) and start typing
Alternatively you can start typing and press (Ctrl + Space) to auto complete.

Dim myVariable As String 
alt text

Duplicate Variables

Variable names can only be declared once.
If you accidentally declare the same variable twice an error will be displayed when you compile your code.

Dim myVariable As String 
Dim myVariable As String
alt text

Reserved Words

Variable names cannot be the same as VBA keywords.
VBA has a large number of "reserved words" that cannot be used for variable names.
Some examples of these are words such as "Sub", "Function", "Array", "Integer", etc.
If you try to use a reserved word for a variable a compile error will be generated.

alt text


It is always a good idea to type VBA keywords in lower case and then wait for them to automatically change.
It is generally good programming practice to put all your declarations at the very top of a subroutine as it improves readability and makes housekeeping a lot easier.
You can actually put your variable declarations anywhere in the code although it is good practice to put them at the start of a subroutine.
Static variables cannot be initialised since that defeats their purpose

© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext