In this article we'll be discussing the Excel VBA variable types. If you're familiar with any other programming language the same basic variables types will be available in VBA.
Variables are also known as the storage of different kinds of data types and constants for your worksheets. We suggest using variables more than constant because you can easily change a variable while constants remain fixed. We have two main types of variables; numerical and non-numerical.
When we store numbers, a numerical data type is used to store those. See below examples of data types:
We also have other types of numerical variables which are byte, decimal, currency, date and time.
See below examples of non-numerical variables:
-Object - this is used to store object data types like workbooks, worksheets, sheets ranges, cells, and more.
-Double - this is used for storing numbers with decimals.
-Date - this is used for storing dates.
-Variant - this is used with both numerical and non-numerical types of data.
We have to follow some rules when naming a variable:
Let's see an example below:
Sub example1()
Dim product_Name As String
product_Name = "Hello!!"
MsgBox product_Name
End Sub
The variable created here was named product_Name and we assigned a string to it with a value of "Hello!!".
Let's take a look a the result after running the code:
We have covered the variable types in VBA and the naming conventions involved in creating a variable. Get familiar with how to use variables because they will help you be more productive when writing your scripts in VBA.