There can be situations when you want to declare a number in your program that you never want to be changed. In other situations, like, as if you have used a person's name many times in your program and you want to change that name, this assigned task could be hectic if you change the name of that person at every single position you have used in your program, but, if you know constants then this task could be completed within seconds with the help of constants. In this article, you will learn how to use constants in excel VBA.
Constants in VBA
Constant is a convenient label for which value doesn't change. Many times you have come across the constants in VBA unknowingly. A major difference between a variable and a constant is that you cannot reassign the value of a constant. For example, if you want to change the color of the cell to read, then you might use VbRed in your VBA macro.
Using constants in your code
- Constants help in writing defensive code so that one cannot change the value of that constant in the future. Consider a situation where you have created a module declaring all the constants and password-protected it. The scope of the constants is workbook level. Another person working in some other module cannot access the constants, thus not changing its value.
- Creating constants makes code more readable. For example, every person might not know what is 9.8 until mentioned its value of acceleration due to gravity.
- Constants save your time. Consider a situation you have used someone's name in the code, and now you want to change it. Changing the name at every position is time taking. You can use constants by which changing the name of that person at the time of constant declaration will change the name in the entire code.
Constants can be of two types
- Built-In Constants
- User-Defined Constants
Built-In Constants
VBA provides 100+ in-built constants to VBA users. There are boolean, color, and date-type-based constants in VBA. For example, vbRed, vbBoolean, and vbDate. For example, given the cell value of C2 as "Arushi practices dsa on geeks for geeks". Your task is to change the color of the cell to black and set the font as green using in-built VBA constants.
Step 1: Open your VBA editor. The name of the procedure is geeks(). Set the color of cell C2 to black. Here, we have used the in-built constant vbBlack to color the cell. The function used to achieve this is Range(cell).Interior.Color.
Step 2: Set the font of the text inside C2 to green by using the VBgreen in-built constant. The function used to achieve this is Range(cell).Font.Color.
Step 3: Run your macro. The text color is changed to green and the background color to black.
Internal Working of Constants
From the above example, it might seem that the constant vbGreen changes your text color green, but that's the half-truth. In reality, the majority of constants are numeric values. The numeric value of vbGreen is 65280. Different Methods to know the numeric value of the constant:
Method 1: Use F8
Step 1: Open your VBA editor. Under a sub procedure. Press Fn + F8, on your keyboard. Now, a yellow line may highlight the subprocedure i.e., geeks().
Step 2: Now, if your hover your cursor on the constant, it will show the numeric value of that constant. For example, the numeric value of vbGreen is 65280.
Method 2: Use print in VBA
You can print the numeric value of the constant in the immediate window itself.
Step 1: Use Debug.Print(constant) in the code. Click on the run.
Step 2: The value of the constant will be printed in the immediate window.
Getting a List of built-in Constants
VBA provides a detailed list of in-built constants. You can access the name and numeric value by referring to the list.
Step 1: Open your VBA code editor. Click on the View Tab.
Step 2: Click on the Object Browser or press F2.
Step 3: A section named Members of '<globals>' is opened. Here we have the list of all the constants in VBA.
Step 4: Click on any of the cells to get the numeric value of that constant.

User-Defined Constants
You can get into situations when you want to define your custom constants. For example, the mathematical constants like PI(3.14), h(plank's constant = 6.626 \times 10^{-34}) etc. Here, you will require the knowledge of user-defined constants.
Converting RGB colors to a numeric value
Before learning how to declare constants in VBA, you need to know how to convert an RGB number to its numeric value. This will be helpful to know which numeric value we have to assign our constant to achieve the specified color. For example, find the numeric value of the custom black color.
Step 1: Go to the Home tab. Under the Font section, click on the theme color. Now, click on More colors.
Step 2: A colors tab is open. Select the custom menu. You can see the value of R, G, and B, i.e., 7, 36, and 2. Click Ok.
Step 3: Now, open your VBA editor. In the immediate window, type the ?RGB(red_value, green_value, blue_value). Press Enter. You will obtain the numeric value of that color i.e., 140295.
Declaring Custom Constants in VBA
The custom constants are declared the same way as variables are declared, but instead of using the 'Dim' keyword, we use the 'Const' keyword in the syntax.
Syntax: Const constant_name as data_type = value_assigned
For example, given the cell value of C2 as "Arushi practices dsa on geeks for geeks". Your task is to change the color of the cell to black and set the font as green using custom VBA constants.
Step 1: Declare the constant black in the sub-procedure scope. As the numeric value of black is 0. So the constant is set to 0.
Step 2: Set the color of the cell to black by using Range(cell).Interior.Color function, assigned by a constant to black.
Step 3: Repeat steps 1 and 2. Declare the numeric value of green, i.e., 65280, which can be found by rgb to the numeric converter as shown above. Set the font color by Range(cell).Font.Color function, assigned by constant to green.
Step 4: The background of cell C2 is set to black and the font color to green.

Scope of Constants in VBA
The constants are declared according to their scopes. Some constants have only sub-procedure scope; some might have module scope etc.
The constant declared in a procedure has its scope within the procedure itself. The constant black declared inside the procedure geek() will not work for any other procedure or module.
The constant is declared outside a procedure, and the scope of the constant is valid for the entire module. That constant can be used in any procedure or sub-procedure, or function.
The scope of the module can be increased to all the modules by using the keyword public while declaring the constant. Now, the scope of constant is for all the modules.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read