Constants

When writing programs, some frequently used characters or numbers can be defined as constants. A constant is a name representing the meaning of these characters or numbers. Using constants improves code readability—for example, using the name PI to represent the circle ratio 3.1415926 makes the meaning clearer and the expression more concise. After a constant’s value is defined, it cannot be changed during program execution. Constants include built-in constants and user-defined constants.

Excel VBA Constants

Built-in constants are pre-defined by Excel VBA, commonly including True, False, Null, Empty, and Nothing. True and False represent logical true and false, respectively, and are the two values of Boolean variables. Null indicates a variant expression is empty and cannot be used for numeric or string variables. Empty indicates a variable has not been initialized (it is empty). Nothing indicates an empty object; after an object-type variable is no longer needed, setting it to Nothing deallocates its memory.

Excel VBA also has other built-in constants, such as vbTab (tab key, equivalent to the string Chr(9)) and vbCrLf (carriage return + line feed, equivalent to Chr(13) & Chr(10)). The following code inserts vbCrLf between two strings to represent a line break:

code.vba
Debug.Print "Hello " & vbCrLf & "VBA!"

The output after running is:

code.vba
Hello
VBA!

The constant vbRed in Excel VBA represents red. The following code sets the background color of cell A1 in the active worksheet to red:

code.vba
ActiveSheet.Range("A1").Interior.Color = vbRed

In Excel VBA, you can also define constants using the Const statement. For example, the following uses the string PI to represent the circle ratio 3.1415926:

code.vba
Const PI = 3.1415926

After defining a custom constant, you can use it as follows:

code.vba
Debug.Print PI * 2

Running this outputs 6.2831852 in the Immediate Window.

You cannot modify a defined custom constant; otherwise, an error message will pop up. For example, the following attempts to change PI to 3.14:

code.vba
PI = 3.14

The error message when running is shown in Figure 2-1:

Python Constants

Common built-in constants in Python include True, False, and None. True and False represent logical true and false, respectively, and are the two values of Boolean variables. None represents an empty object (i.e., a missing object). The values of built-in constants cannot be changed during code execution. For example, the following code attempts to change True to 3, which returns a syntax error:

code.python
>>> True
True
>>> True = 3
SyntaxError: can't assign to keyword

For convenience, some built-in or third-party modules pre-define constants. For example, the commonly used math module pre-defines the circle ratio pi and natural exponent e. To use the math module, first import it with the import statement:

code.python
>>> class="text-secondary">import math
>>> math.pi
3.141592653589793
>>> math.e
2.718281828459045

By default, Python does not support custom constants. When you need to define a constant, it is common to use all uppercase letters for the variable name to indicate it is a constant:

code.python
>>> SMALL_VALUE = 0.000001
>>> SMALL_VALUE
1e-06

Constants defined this way are essentially variables because their values can be modified during code execution:

code.python
>>> SMALL_VALUE = 0.00000001
>>> SMALL_VALUE
1e-08

Thus, using all uppercase letters for variable names is a convention. When we see or use them, we know they are constants and should not be modified.

In fact, the pre-defined constants pi and e in the math module are also variables because their values can be changed:

code.python
>>> math.pi = 3
>>> math.pi
3