Excel VBA Variables, Data Types & Constant
Excel VBA Variables, Data Types & Constant
Excel VBA Variables, Data Types & Constant
Home
Testing
SAP
Web
Must Learn!
Big Data
Live Projects
Blog
VBA Variables
VBA Data-Types
Constant in VBA
VBA Variables
Variables are specific values that are stored in a computer
memory or storage system. Later, you can use that value in
code and execute. The computer will fetch that value from the system and show in the output. Each variable
must be given a name.
To name the variable in VBA, you need to follow the following rules.
Here are some example for Valid and Invalid names for variables in VBA.
My_Watch My.Watch
https://www.guru99.com/vba-data-types-variables-constant.html 1/11
3/6/2018 Excel VBA Variables, Data Types & Constant
In VBA, we need to declare the variables before using them by assigning names and data type.
Explicitly: Below is an example of variable declared Explicitly. You can use "Dim" keyword in syntax
Dim Num As Integer
Dim password As String
VBA variable is no different than other programming languages. To declare a variable in VBA you use the
keyword "Dim."
Before we execute the variables we have to record a macro in Excel. To record a macro do the following -
Step 3): Open the Macro editor, enter the code for variable in the Macro1
When you run this code, you will get the following output in your sheet.
https://www.guru99.com/vba-data-types-variables-constant.html 2/11
3/6/2018 Excel VBA Variables, Data Types & Constant
In VBA, if the data type is not specified, it will automatically declare the variable as a Variant.
Let see an example, on how to declare variables in VBA. In this example, we will declare three types of
variables string, joining date and currency.
Step 1) Like, in the previous tutorial, we will insert the commandButton1 in our Excel sheet.
https://www.guru99.com/vba-data-types-variables-constant.html 3/11
3/6/2018 Excel VBA Variables, Data Types & Constant
Step 2) In next step, right-click on the button and select View code. It will open the code window as shown
below.
https://www.guru99.com/vba-data-types-variables-constant.html 4/11
3/6/2018 Excel VBA Variables, Data Types & Constant
Step 5) After turning off the design mode, you will click on commandButton1. It will show the following
variable as an output for the range we declared in code.
Name
Joining Date
Income in curreny
Constant in VBA
https://www.guru99.com/vba-data-types-variables-constant.html 5/11
3/6/2018 Excel VBA Variables, Data Types & Constant
Constant is like a variable, but you cannot modify it. To declare a constant in VBA you use keyword Const.
You can either specify the scope as private by default or public. For example,
Summary:
Variables are specific values that are stored in a computer memory or storage system.
You can use "Dim" keyword in syntax to declare variable explicitly
VBA data types can be segregated into two types
Numeric Data Types
Non-numeric Data Types
In VBA, if the data type is not specified. It will automatically declare the variable as a Variant
Constant is like a variable, but you cannot modify it. To declare a constant in VBA you use keyword
Const.
Prev
Report a Bug
Next
https://www.guru99.com/vba-data-types-variables-constant.html 6/11
3/6/2018 Excel VBA Variables, Data Types & Constant
VBA Tutorial
1) How to Write Macros in Excel
2) Visual Basic for Applications
3) Data Types, Variables, Constant
4) VBA Arrays
5) VBA Excel Form Control
6) VBA Arithmetic Operators
7) VBA String Operators
8) VBA Comparison Operators
9) VBA Logical Operators
10) Excel VBA: Call a Subroutine
11) Excel VBA Function
12) VBA Range Objects
Top Tutorials
About
About US
Advertise with Us
Write For Us
Contact US
Career Suggestion
SAP Career Suggestion Tool
Software Testing as a Career
Top Tools List
Certificates
Selenium
Testing
https://www.guru99.com/vba-data-types-variables-constant.html 7/11
3/6/2018 Excel VBA Variables, Data Types & Constant
Hacking
Interesting
Books to Read!
Suggest a Tutorial
Blog
Quiz
SAP
Java
Python
Execute online
Execute Java Online
Execute Javascript
Execute HTML
Execute Python
Jmeter
https://www.guru99.com/vba-data-types-variables-constant.html 8/11
3/6/2018 Excel VBA Variables, Data Types & Constant
Informatica
JIRA
https://www.guru99.com/vba-data-types-variables-constant.html 9/11
3/6/2018 Excel VBA Variables, Data Types & Constant
https://www.guru99.com/vba-data-types-variables-constant.html 10/11
3/6/2018 Excel VBA Variables, Data Types & Constant
https://www.guru99.com/vba-data-types-variables-constant.html 11/11