VBA Const
Last Updated :
-
Blog Author :
Edited by :
Reviewed by :
Table Of Contents
What is VBA Const (Constants)?
Variables are the heart and soul of any programming language. We have never seen a coder or developer who does not rely on variables in their project or program. As a coder, no one is not different from others. We use variables 99% of the time. We all use the "Dim" statement; we declare VBA variables. Our articles have shown you about declaring variables through the "Dim" statement. But we declare variables using another way as well. This article will show you the alternative route of declaring variables, i.e., the "VBA Constant" method.
"Const" stands for "Constants" in VBA. Using the VBA "Const" word, we can declare variables like how we declare variables using the "Dim" keyword. We can display this variable at the top of the module, between the module, in any subroutine in VBA and function procedure, and the class module.
To declare the variable, we need to use the word "Const" to display the constant value. Once the variable is declared and assigned a cost, we cannot change the weight throughout the script.
Syntax of Const Statement in VBA
The Const statement is slightly different from the "Dim" statement. To understand it better, let us look at the well-written syntax of the VBA Const statement.
- Const: With this word, we initialize the process of declaring the constants.
- Variable Name: This is as usual as naming the variable. We rather call it Const Name instead of Variable Name.
- Data Type: What kind of value our declared variable is going to hold.
- Variable Name: Next and final part is what is the value we are going to assign to the variable we have declared. The given weight should be as per the data type.
Condition of Constants in VBA
- The name of the constant we are declaring can contain a maximum of 256 characters in length.
- The constant's name cannot start with a number; rather, it should begin with the alphabet.
- We cannot VBA reserved keywords to declare the constants.
- The constant name should not contain any space or special characters except the underscore character.
- One can declare multiple constants with a single statement.
Examples of Const Statement in VBA
Let's declare your first variable through the VBA Const statement. After that, we can declare constants at the subprocedure, module, and project levels.
Now, look at how to declare at the sub procedure level.
In the above example, we declared constant "k" inside the sub procedure named Const_Example1(). And we have assigned the value as 75.
Now, look at the module level Constant declaration.
At the top of the module, we have declared three constants in the module "Module 1".
These VBA constants can be accessed in "Module 1" at any sub procedures within this module, i.e., "Module 1."
Make Constants Available Across Modules
We can access those constants within the module with all the subprocedures when we declare the constants at the top of the VBA class module.
But how can we make them available with all the modules in the workbook?
To make them available across modules, we need to declare them with the word "Public."
Now, the above variable is not only available with "Module 1." Instead, we can use "Module 2" as well.
Difference Between VBA Dim Statement & Const Statement
It would help if you doubted the difference between the traditional "Dim" statement and the new "Const" statement in VBA.
We have one difference with these, i.e., look at the below image.
In the first image, as soon as we declare a variable, we have assigned some values to them.
But in the second image, using the "Dim" statement first, we have declared variables.
After declaring a variable, we have assigned values separately in the different lines.
Like this, we can use the VBA "Const" statement to declare constants, which is a similar way of communicating variables with the "Dim" statement.
Recommended Articles
This article has been a guide to VBA Const (Constants). Here we discuss how to use the Constant statement in VBA along with examples and its critical differences with Dim information. Below are some useful Excel articles related to VBA: -