VBA Variables, Data Types and Constants in Excel
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 kywords 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.
Example:
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 categorised 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.
Example:
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.
Example:
Const MyVal As integer Const MyVal=10 Where Const represnts the constant variable and it has value 10.
Excel VBA Tutorial:
Please click on below link to go back to see Excel VBA Tutorial table of content.