Open In App

VBA Constants in Excel

Last Updated : 24 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

  1. 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.
  2. 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.
  3. 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

  1. Built-In Constants
  2. 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.

C2-cell-value-filled

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.

Using-in-built-constant-vbBlack

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.

Using-vbGreen-in-built-constant

Step 3: Run your macro. The text color is changed to green and the background color to black.

Running-macro

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().

Highlighting-in-yellow

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.

Numeric-value-of-vbGreen

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.

Printing-numeric-value

Step 2: The value of the constant will be printed in the immediate window.

Value-printed-in-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.

Clicking-view

Step 2: Click on the Object Browser or press F2.

Clicking-object-browser

Step 3: A section named Members of ‘<globals>’ is opened. Here we have the list of all the constants in VBA.

List-of-constants-in-VBA

Step 4: Click on any of the cells to get the numeric value of that constant.

Obtaining-numeric-value-of-cells

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.

Clicking-theme-color

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.

Selecting-custom-menu

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.

Obtaining-numeric-value-of-that-color

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.

Changing-color-in-cell

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.

Constant-set-is-0

Step 2: Set the color of the cell to black by using Range(cell).Interior.Color function, assigned by a constant to black.

Assigning-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.

Declaring-custom-constant

Step 4: The background of cell C2 is set to black and the font color to green.

Cell-color-set-black-and-font-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.

  • Scope in a procedure

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.

Declaring-custom-constant-for-black-color
  • Scope in a 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.

Scope-in-a-module
  • Scope in all modules

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.

Scope-in-all-modules

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads