Stop searching. Start learning and developing your excel skills.
Macro
VBA
Formula
Function
Shortcut
Tricks

» » VBA Variables

VBA Variables

August 10, 2017 |
Dim is a short form of Dimension.

It is used to declare variable names and their type.
It is always used at the beginning of macro codes (not compulsory) with format:

Dim [Insert Variable Name] as [Insert Variable Type]

There are many variables in VBA. I just list down most used variables type as below:

1. String - It stores values or numbers, but treats them as text that range from 1 to 65,400 characters

2. Integer - It stores only Integer (no decimals) that range from –32,768 to 32,767
3. Long - It stores only Integer (no decimals), but the range is much larger than Integer data type. It stores values from –2,147 Mil to 2,147 Mil

4. Single - It stores numerical values that have digits to the right of the decimal point. Limited to 7 digits.
5. Double - Same with single but can upto 15 digits. Perfect explanation here

6. Date - It stores a date value

7. Variant - It holds any type of value you want. It is universal (or vague) data type you can use for any type of value

How to declare your variables? Well, just follow the standard format:

Dim [Insert Variable Name] as [Insert Variable Type]
Example:
Dim Sample as String

I strongly recommended you to declare all your variables to avoid error.

You could use Option Explicit to force you to declare all variables by turning on 'Require Variable Declaration'. It is located in VBA Editor → Tools → Options → Editor tab.

Or you just need to be discipline to declare by your own if you do not like Option Explicit.

No comments:

Post a Comment