What are Constants in VBA?

Constant is a named memory location used to hold a value that CANNOT be changed during the script execution. If a user tries to change a Constant value, the script execution ends up with an error. Constants are declared the same way the variables are declared.

Following are the rules for naming a constant.

  • You must use a letter as the first character.
  • You can’t use a space, period (.), exclamation mark (!), or the characters @, &, $, # in the name.
  • Name can’t exceed 255 characters in length.
  • You cannot use Visual Basic reserved keywords as a variable name.

Syntax

In VBA, we need to assign a value to the declared Constants. An error is thrown, if we try to change the value of the constant.

Const <<constant_name>> As <<constant_type>> = <<constant_value>>