WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Free Tutorials
  • Certification Courses
  • Excel VBA All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA Const

VBA Const

What is VBA Const (Constants)?

Variables are the heart and soul of any programming language. I have never seen a coder or developer who doesn’t rely on variables in their project or program. As a coder, even I am not different from others, and I use variables 99% of the time. We all using the “Dim” statement; we declare VBA variables. In our articles, we have shown you about declaring variables through the “Dim” statement. But we declare variables using another way as well. In this article, we 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 VBA 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.

VBA Constants

Syntax of Const Statement in VBA

Const statement is slightly different than the “Dim” statement. To understand it better, let look at the well-written syntax of the VBA Const statement.

Const [Variable Name] As [Data Type] = [Variable Value]
  • 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 of length.
  • The name of the constant 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 underscore character.
  • Multiple constants can be declared with a single statement.

Examples of Const Statement in VBA

Let declare your first variable through the VBA Const statement. We can declare constants at the subprocedure level, module level, and at the project level as well.

Now, look at how to declare at the Sub Procedure level.

VBA Const Example 1

In the above example, constant “k” is declared inside the subprocedure named Const_Example1(). And we have assigned the value as 75.

Popular Course in this category
Sale
VBA Training (3 Courses, 12+ Projects)
4.6 (247 ratings)
3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
View Course

Now, look at the module level Constant declaration.

Example 1-1

At the top of the module, I have declared three constants in the module “Module 1”.

These VBA constants can be accessed in the “Module 1” at any Sub Procedures within this module i.e., “Module 1”.

Make Constants Available Across Modules

Once the constants are declared at the top of the VBA class module, we can access those constants within the module with all the subprocedures.

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

Example 2

Now the above variable not only available with Module 1. Instead, we can use them with Module 2 as well.

Difference Between VBA Dim Statement & Const Statement

It would help if you had a doubt what the difference between the traditional “Dim” statement and the new “Const” statement in VBA is.

We have one difference with these i.e., look at the below image.

Dim & Const Statement Differences

Dim & Const Statement Differences 1

In the first image, as soon 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.

This is how we can use the VBA “Const” statement to declare constants, which are a similar way of communicating variables with the “Dim” statement.

Recommended Articles

This has been a guide to VBA Const (Constants). Here we discuss how to use Constant statement in VBA along with examples and also its critical differences with Dim information. Below are some useful excel articles related to VBA –

  • VBA Do Loop
  • VBA DoEvents
  • VBA Option Explicit
  • VBA Text
0 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

New Year Offer - VBA Training Course (6 courses, 35+ hours video) View More