Data Types
Data types in Excel Visual Basic for Applications differentiate numbers from letters. Data types also differentiate simple integer numbers from decimal numbers. Knowing the different data types will enable you to make the right choose when declaring a variable with DIM.
Here are some basic variable declarations with the main data types in VBA.
Dim variable as string
Dim variable2 as integer
Dim variable3 as long
Dim variable4 as double
Dim variable5 as date
Variables do not need to be declared, but declaring them with "DIM variable as string" or "DIM variable as double" or "DIM variable3 as integer" will set a certain characteristic to the variable that is unique. Different data types have different characteristics. Keeping these characteristics in mind will help you to customize your code to save space, enhance code processing speed, and prevent "rookie" errors that can easily be avoided by reading the below.
Data Types
1. Integer – Declares the variable as a number between and including -32,767 to 32,767. This saves for processing if you are working with whole numbers. It will automatically remove any decimal points from the variable. If you want to work with larger numbers and decimal points, then I recommend the Double data type.
2. String – Declares the variable as letters. For instance, DIM blue as string will declare the variable blue as a string data type which can be set to equal any combination of letters or letter values. You cannot set letter variables = #N/A, so I normally overwrite #N/As caused by vlookup non-matches with a new string such as "No" and then match off the variable to those letters.
Dim blue as string
blue = "no"
msgbox(blue)
3. Double – Declares the variable as an integer with decimals up to 14 decimal points of precision 0.00000000000000. The number can be astronomically large, as large as four times the size of the below number: 1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.
The actual min max of double data types are the below
-1.79769313486231E308 to -4,94065645841247E-324 for negative values and
from 4,94065645841247E-324 to 1,79769313486232E308 for positive ones
4. Long – Basically Integer. The only difference is that Long rounds up or down to nearest whole number. So if the variable is equal to 3.5 then it will process as 4. If the variable is 3.4, then it will process as 3. The integer just straight up removes the decimal point.
5. Date – Let's you declare a date variable such as June 19, 2017.
For a video walkthrough of the different data types in excel VBA, please watch my below video. I recommend watching in the Youtube mobile app for best quality. If you are on desktop, then you can select the little gear in the bottom right hand corner of the video to 1080p for best video quality.