In the previous lessons, we explored variables and how to declare them in VBA. In this section, we’ll discuss the concept of a Constant and see why and how you should use constants in your VBA code.
What Is a Constant in VBA?
Sometimes, we use values in VBA that remain the same throughout the program and appear repeatedly — for example, a tax rate, a profit percentage, or a specific file path. In such cases, it’s better to use constants instead of writing numbers or text directly into the code.
Using constants makes your code easier to read, more maintainable, and less error-prone. Essentially, a constant provides a meaningful name for a value that does not change while the program is running.
For example, if you write the tax rate directly in several places in your code and later the rate changes, you would need to review and update the entire project. However, if you use a constant, you only need to update its value in one place.
Unlike variables, the value of a constant cannot be changed while the program is running.
According to the VBA Glossary, a constant in VBA is a named element whose value does not change during program execution.
Types of Constants in VBA
- Intrinsic Constants: These are built-in constants provided by VBA or its libraries (such as Excel, Access, or Word). You can view them using the Object Browser.
- Symbolic Constants: These are constants you define yourself using the
Conststatement. This is the main focus of this tutorial. - Conditional Compiler Constants: These are defined using the
#Constdirective and are used to control parts of the code at compile time.
How to Declare a Constant in VBA
To define a constant, use the Const keyword. The general syntax is as follows:
Const ConstantName As DataType = Value
For example, suppose you want to specify a tax rate as a constant in your program:
Public Const VAT_RATE As Double = 0.09
In this example, the public constant VAT_RATE represents a 9% tax rate, and it can be used anywhere in your VBA project.
Constant Scope in VBA
You can declare constants either at the procedure level or at the top of a module. If you declare a constant at the top of a module, it is Private by default.
| Constant Type | Scope | Accessible From |
|---|---|---|
Private Const | Module-level only | Procedures and functions within the same module |
Public Const | Global | All modules and forms |
Difference Between a Constant and a Variable
- The value of a constant is assigned once when declared and cannot be changed later.
- A variable is declared using
Dimand its value can change during program execution. - Using constants helps reduce errors and improves code readability.
Declaring Multiple Constants in One Line
You can declare several constants in one Const statement, but the data type of each constant must be specified separately:
Const conAge As Integer = 34, conWage As Currency = 35000
If a constant’s value is a string, it must be enclosed in quotation marks (" "). For example:
Const COMPANY_NAME As String = "IranVBA Academy"
Frequently Asked Questions about Constants in VBA
What are constants used for in VBA?
Constants in VBA are used to define values that remain the same during program execution, such as tax rates or profit percentages. This improves readability and reduces coding errors.
What’s the difference between a Constant (Const) and a Variable (Dim) in VBA?
Variables can change at runtime, whereas constants remain fixed after declaration. The Dim statement declares variables, while Const declares constants.
How can I declare a global constant in VBA?
Use the Public keyword before Const. For example: Public Const VAT_RATE As Double = 0.09. This constant will be accessible across all modules and forms.
Can I change a constant’s value while the program is running?
No. Once declared, a constant’s value cannot be changed. If you need a value that can vary at runtime, use a variable instead.
Can I define multiple constants on one line?
Yes, you can define multiple constants in a single Const statement, but each constant must specify its data type. For example: Const conAge As Integer = 34, conWage As Currency = 35000.
What are Conditional Compiler Constants (#Const)?
Conditional Compiler Constants are defined with the #Const directive and are used to include or exclude code sections at compile time, not runtime.
References (Microsoft Docs):
Declaring Constants
Using Constants in VBA
Read More
Variable Scope in VBA | How to Access Variables across Different Parts of a Project
VBA Modules | Types of Modules and the Difference Between a Module and a Class
Operator Precedence in VBA | Order of Arithmetic and Logical Operations with Examples
VBA Operators | Performing Data Operations and Building Expressions
Variables in VBA | How to Declare Variables and Methods
VBA Code Editor | Create, Edit and Save VBA Codes
Introduction to VBA Code Structure: From Zero to Your First Function