Constants

If you need to use a value that never changes, it will be wise to use a constant instead of a variable. When you use the constant you can be sure that the value is always the same and you won’t accidentally change it to a different one. If you try, Excel will return an error.

You can declare constants by using the Const word at the beginning.

Excel VBA Constants

Besides user-defined constants, Excel offers many predefined ones. You can meet them when you use the macro recorder.

Look at the following code generated by the macro recorder. It fills a cell with yellow color.

Insert MsgBox(xlAutomatic) before End With.

If you execute the macro it will display the message showing the value assigned to constant xlAutomatic.

If you write .PatternColorIndex = -4105 instead of xlAutomatic it will give you the same result.

Tomasz Decker is an Excel specialist, skilled in data analysis and financial modeling.