Vba declaring assigning variable datatype in Excel
Variables in VBA are "named memory locations" in the computer that stores a value of a specific data type.
Variables can be of any data type such as Text, Number, Decimal, Charater or Object.
Variables should be "Declared" and "Assigned" to use it in the script or subroutines.
Variables are declared using DIM keyword and assigned values using =
Declaring Variables using DIM keyword
- Sub HelloWorld()
- Dim variable As Integer
- Dim str As String
- Dim dbl As Double
- Dim obj As Object
- Dim flag as Boolean
- End Sub
Here is how it looks in the editor
Assigning Values to the variables
Here, the variable str holds a value of "Hello VBA", dbl holds "98.87" and variable holds "10"
These values can be accessed anywhere within the script and can be modified anytime
You can find similar Excel Questions and Answer hereunder
1) How to format variable with VBA with the very useful format function. Define the number of decimal, the way a date is formated
2) What is variable scope in VBA, how do variable can be reach from other function or routines in VBA, Public and Private Variable in VBA
3) How can I hide a specific Shape in my WorkSheet?
4) How can I prevent other users from adding or deleting Sheets in file?
5) How can I shade alternate rows in color using VBA to make it easier to read voluminous data running into hundreds of rows?
6) How can I turn off Alerts using VBA?
7) One of my formulas has returned an error - how can I trace the error to fix it?
8) I am not able to use VLOOKUP as the lookup value column is to the right of the column that has the information I need. Is there an alternative option?
9) What is variable scope in VBA, how do variable can be reach from other function or routines in VBA
10) How to use the name manager in Excel to have a good overview of the various variables and their names in Excel