Abdul Alim: a little bit learning, let's go
Excel, VBA and Power BI tutorials
Abdul Alim: a little bit learning, let's go
Excel, VBA and Power BI tutorials
In this chapter you will learn how to declare a variable and different type of data type in VBA.
Variables are used to store the information. We can re-assign a value on a variable.
See the below example
Sub Use_Variable()
Dim my_age As Integer
my_age = 30
MsgBox my_age
End Sub
Dim keyword is used to declare a variable. Integer is a data type.
Declaring these variables is not absolutely necessary, but it is recommended. We should get in the habit of declaring variables correctly. Try to keep your variable name properly.
We can make our variable declaration mandatory by using Option Explicit in the starting of the module.
To add automatically Option Explicit while adding a new module-
Go to Visual Basic Editor Window >>Tools>>Options>>check the Require Variable Declaration
Options Window in Visual Basic Editor
Tick on Require Variable Declaration check box.
Now if you insert a new module, Option Explicit will be there automatically.
Option Explicit in module
As in above example we have taken my_age as an Integer. Basically Integer is a data type, which is used for numbers.
Below is the list of data type in VBA.