Share this Page URL

A VBA Language Primer > Conditional Statements - Pg. 417

static throughout the course of your program. Constants help document a program by replacing often-used values with a more meaningful label. Constants are also very useful for defining values that are used in more than one location in your VBA code, enabling easier code modification later. In Word VBA, you'll probably end up using Word's predefined constants more often than defining your own. Declare a constant using the Const keyword, as shown in Example 18-11. Example 18-11. Declaring a Constant Const WordProcessor = "Word" Const NumFingers = 10 Declare All Constants at the Beginning of Code It's best to declare all constants that you'll use in your VBA code in one spot (usually at the beginning), so you'll always know where to find them. Use a constant just as you would use a variable, except that you can't assign a value (Example 18-12). Example 18-12. Using a Constant MsgBox "I have " & NumFingers & " fingers."