VBA Variables, Data Types and Constants in Excel.Variables store values in a computer memory. Data types represents the what kind of data. Constants represents values.The variable values can change but constant values cannot change. All variables and constants consists of data type.
VBA Variables in Excel
We have to follow different instructions while naming a variable. Let us what are those instructions.
Rules to Name a Variable in VBA
We must follow the following rules while declaring a variable name.
- Variable Name must be less than 255 characters long
- VBA keywords or reserved words cannot use as a Variable Name
- It should not begin with number, always must begin with a letter
- Variables consists of only letters, numbers and underscore characters.
- It doesn’t allow space in the variable name.
- Variable name doesn’t allow period(.)
- We cannot use special characters like @,#,$.%,&,!, etc.
Dim sName Where sName represents a variable name.
VBA Data Types in Excel
We have many data types in MS Office Excel VBA. We have categorized data into two types. Those are numeric data type and non-numeric data types. Data types we use with Dim statement to declare variables. Declaring variables and its data types helps programmer and avoid mistakes. If data type is not specified, default it considers variant data type. It occupies more memory.
Dim sName as string Where string represents the data type
Read more about VBA Data Types
VBA Constants in Excel
Constants in VBA helps to programmer to declare and assign value to use in module. Constant also like a variable but cannot change constant value. We use’Const’ keyword to declare constants. You can use either private or public while declaring constants.
We have two types of constants.
1. Built-in Constants
2. User defined constants.
Const MyVal As integer Const MyVal=10 Where Const represents the constant variable and it has value 10.
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers