A convenient way to declare constants in VBA

It is good programming practice to use constants for fixed values instead of littering your code with value literals. Firstly, you prevent having the same value being used in multiple places with the risk of forgetting to change each causing confusion. Secondly, it allows you to give a separate location to keep a group of constants which you know that is the place where you can set the important values.

The Constant Builder included in Code VBA makes specifying constants very easy and efficient.

  • By default it opens when you type Const in an appropriate place: the module header or inside a procedure.
  • In case of String constants, which are the most common, it let's you select a value using Name selection continuation menu.
  • If inside a procedure, you can indicate if you want the constant to appear at the cursor position or at the top of the procedure.
  • If you want to declare multiple constants in one go it can do so using the Insert button which keeps the constant builder dialog open.
  • In the module header you can indicate the constants accessibility:
    • Public: also available to other modules.
    • Private: availability limited to the current module.
Constant Builder started in module header
Constant Builder started in module header
Constant Builder started inside a procedure
Constant Builder started inside a procedure