📌 Constants in VBA
Learn how to create fixed values in VBA, understand constant types, scope, naming conventions, built-in constants, and how constants make your VBA programs easier to maintain.
BEGINNER → INTERMEDIATE📘 What is a Constant in VBA?
A constant is a value that is defined once and is not intended to change while the VBA program is running.
For example, suppose your application uses a fixed tax rate
of 18%.
Instead of writing 18% repeatedly throughout your
code, you can create a constant.
A constant is a named value whose value remains fixed during program execution.
💻 Basic Constant Example
The basic syntax for declaring a constant is:
Now you can use the constant anywhere within its available scope.
The result will be:
🧩 Constant Syntax
For example:
| Part | Meaning |
|---|---|
| Const | Keyword used to declare a constant. |
| constantName | Name given to the constant. |
| As | Specifies the data type. |
| DataType | String, Long, Double, Boolean etc. |
| value | Fixed value assigned to the constant. |
⚖️ Constant vs Variable
The main difference is whether the value is expected to change.
| Feature | Variable | Constant |
|---|---|---|
| Declaration | Dim | Const |
| Value can change? | Yes | No |
| Example | Dim taxRate As Double | Const TAX_RATE As Double = 0.18 |
| Typical use | Changing data | Fixed values |
Variable Example
A variable can be changed.
Constant Example
You cannot intentionally assign a different value later.
This will cause an error:
TAX_RATE = 0.20
🎯 Why Should You Use Constants?
Constants are especially useful when the same fixed value is used repeatedly throughout a VBA application.
Without a Constant
If the tax rate changes, you may need to modify many places.
With a Constant
Now the rate is maintained in one place.
If the value changes in the future, you normally update the constant declaration rather than searching through the entire program for every occurrence of the old value.
🏷️ Naming Constants
Good naming makes VBA code much easier to understand.
A common convention is to use uppercase letters with underscores between words.
| Constant | Purpose |
|---|---|
| MAX_ROWS | Maximum number of rows. |
| TAX_RATE | Tax percentage. |
| COMPANY_NAME | Company name. |
| REPORT_FOLDER | Report storage location. |
Choose a name that explains the purpose of the constant. Avoid names such as
X, A1 or
VALUE1.
🔢 Constant Data Types
Constants can be declared using different data types.
| Type | Example |
|---|---|
| Long | Const MAX_ROWS As Long = 1000 |
| Double | Const RATE As Double = 0.18 |
| String | Const COMPANY As String = "ChiragCoder" |
| Boolean | Const DEBUG_MODE As Boolean = True |
📍 Procedure-Level Constant
A constant can be declared inside a Sub or Function.
In this example, the constant is available only inside the procedure where it was declared.
The fixed value is required only by one specific procedure.
📂 Module-Level Constants
If several procedures need the same constant, you can declare it at module level.
Both procedures can use the same constant.
🌐 Public Constants
A constant can also be declared as Public in a
standard module when you want it to be accessible from
other modules in the VBA project.
Another module can then use:
They are useful for application-wide settings, but too many global values can make a large application harder to manage.
🔒 Private Constants
You can explicitly declare a constant as Private.
This limits the constant to the appropriate module scope.
Private constants are useful when the value is only relevant to the internal logic of a particular module.
⚙️ Built-in VBA Constants
VBA and the Office object libraries already provide many constants that you can use in your programs.
For example:
Here, vbYesNo and vbQuestion are
built-in constants provided by VBA.
Common Examples
| Constant | Purpose |
|---|---|
| vbYes | Represents the Yes response. |
| vbNo | Represents the No response. |
| vbOK | Represents the OK response. |
| vbCancel | Represents the Cancel response. |
| vbQuestion | Displays a question icon in a message box. |
| vbInformation | Displays an information icon. |
🚀 Practical Example – Invoice Calculation
Suppose you are creating an invoice application and the tax rate is fixed at 18%.
The advantage is that the business rule is clearly visible:
If your application later needs a different rate, you can change the constant rather than searching through many lines of calculation code.
🏢 Constants in Real-World VBA Projects
Professional VBA applications often contain many fixed values. Constants can make these values easier to identify and maintain.
These values can then be used throughout the application.
🪄 What is a "Magic Number"?
A magic number is a fixed value placed directly inside code without explaining what the value represents.
Example
A developer reading the code may not immediately know what
0.18 represents.
Better Approach
The code now clearly communicates the purpose of the value.
📊 Constant vs Excel Cell
An important design decision is whether a fixed value should be stored as a VBA constant or in an Excel worksheet.
| Use Constant When | Use Excel Cell When |
|---|---|
| The value is part of the program logic. | The value should be changed by users. |
| The value rarely changes. | Business users need to update it. |
| The value is a technical setting. | The value is a business configuration. |
| You want the value protected inside code. | You want the value visible in the workbook. |
Example
A maximum number of retry attempts could be a VBA constant:
But a tax rate that business users regularly change may be better stored in a configuration cell.
❌ Common Mistakes with Constants
- Trying to change a constant after it has been declared.
-
Using unclear names such as
XorVALUE1. - Creating too many Public constants unnecessarily.
- Hard-coding the same value repeatedly instead of creating a meaningful constant.
- Using a constant when the value actually needs to be changed by the end user.
🏆 Best Practices
- Use meaningful constant names.
- Use uppercase naming for important fixed values.
- Specify an appropriate data type.
- Use constants to eliminate magic numbers.
- Use module-level constants when multiple procedures need them.
- Use Public constants only when application-wide access is required.
- Do not use constants for values that users need to change regularly.
📌 Constants – Quick Summary
| Question | Answer |
|---|---|
| What is a constant? | A named value that is not intended to change during program execution. |
| Keyword? | Const |
| Can a constant be changed? | No. |
| Can constants have data types? | Yes. |
| Can constants be declared inside procedures? | Yes. |
| Can constants be shared between modules? | Yes, using appropriate module-level scope such as Public. |
| Why use constants? | Readability, consistency and easier maintenance. |
🎯 Key Takeaway
Instead of scattering numbers and text throughout your application, define important fixed values once and give them descriptive names.
This becomes especially valuable when you start building larger VBA applications such as invoice systems, inventory systems, reporting tools and UserForm-based applications.
🎉 Level 2 Completed!
You have now covered the major fundamentals of VBA variables and data types:
- 📦 Variables
- 🔤 String
- 🔢 Integer & Long
- 📊 Double
- 📅 Date
- ☑️ Boolean
- 🔄 Variant
- 📌 Constants
Now you are ready to move from Variables & Data Types to the next major VBA concept: Operators & Expressions.
0 Comments