Microsoft has recently introduced a structure into VBA to categorize the plethora of symbolic constants. This structure is called an enum , which is short for enumeration. A list of enums can be obtained using my Object Model Browser software. For instance, among Excel's 152 enums, there is one for the fill type used by the AutoFill method, defined as follows:
Enum XlAutoFillType xlFillDefault = 0 xlFillCopy = 1 xlFillSeries = 2 xlFillFormats = 3 xlFillValues = 4 xlFillDays = 5 xlFillWeekdays = 6 xlFillMonths = 7 xlFillYears = 8 xlLinearTrend = 9 xlGrowthTrend = 10 End Enum
(The Excel documentation incorrectly refers to this enum as XlFillType.) Note that enum names begin with the letters Xl (with an uppercase X ).
Thus, the following line of code will autofill the first seven cells in the first row of the active sheet with the days of the week, assuming that the first cell contains the word Monday:
ActiveSheet.Range("A1").AutoFill ActiveSheet.Range("A1:G1"), xlFillDays
This is far more readable than:
ActiveSheet.Range("A1").AutoFill ActiveSheet.Range("A1:G1"), 5
Note that this enum is built in, so we do not need to add it to our programs in order to use these symbolic constants. (We can create our own enums, but this is generally not necessary in Excel VBA programming, since Excel has done such a good job of this for us.)
As another example, the built-in enum for the constant values that can be returned when the user dismisses a message box (by clicking on a button) is:
Enum VbMsgBoxResult vbOK = 1 vbCancel = 2 vbAbort = 3 vbRetry = 4 vblgnore = 5 vbYes = 6 vbNo = 7 End Enum
For instance, when the user hits the OK button on a dialog box (assuming it has one), VBA returns the value vbOK. Certainly, it is a lot easier to remember that VBA will return the symbolic constant vbOK than to remember that it will return the constant 1. (We will discuss how to get and use this return value later.)
VBA also defines some symbolic constants that are used to set the types of buttons that will appear on a message box. These are contained in the following enum (which includes some additional constants not shown):
To illustrate, consider the following code:
If MsgBox("Proceed?", vbOKCancel) = vbOK Then
' place code to execute when user hits OK button Else
' place code to execute when user hits any other button End If
In the first line, the code MsgBox("Proceed?", vbOKCancel) causes Excel to display a message box with an OK button and a Cancel button and the message "Proceed?", as shown in Figure 5-1.
Figure 5-1. Example message box
If the user clicks the OK button, Excel will return the constant value vbOK; otherwise it will return the value vbCancel. Thus, the If statement in the first line will distinguish between the two responses. (We will discuss the If statement in detail in Chapter 8. Here, we are interested in the role of symbolic constants.)
In case you are not yet convinced of the value of symbolic constants, consider the following enum for color constants:
Enum ColorConstants vbBlack = 0 vbBlue = 16711680 vbMagenta = 16711935 vbCyan = 16776960 vbWhite = 16777215 vbRed = 2 55 vbGreen = 65280 vbYellow = 65535 End Enum
Consider which you'd rather type, this:
ATextBox.ForeColor = vbBlue or this:
ATextBox.ForeColor = 16711680 Need I say more?
Was this article helpful?