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 variablesDeclare VBA VariablesVariable declaration is necessary in VBA to define a variable for a specific data type so that it can hold values; any variable that is not defined in VBA cannot hold values.read more. 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 VBASubroutine In VBASUB in VBA is a procedure which contains all the code which automatically gives the statement of end sub and the middle portion is used for coding. Sub statement can be both public and private and the name of the subprocedure is mandatory in VBA.read more 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.

You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA Const (wallstreetmojo.com)

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 moduleVBA Class ModuleUsers have the ability to construct their own VBA Objects in VBA Class Modules. The objects created in this module can be used in any VBA project.read more.

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.

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: –

  • VBA Do LoopVBA DoEventsVBA Option ExplicitVBA Text