Introducing the Enum Statement
The Enum statement declares enumerations or enums. An Enum declaration is a block that starts with an Enum statement and ends with an End Enum statement. The block contains one or more named constants, or assignments of values or expressions (of Long data type) to constants. The header image above shows the Enum statement’s syntax.
Salient Points on Its Usage
There are several vital things to note about the Enum statement:
- Enums are strictly module-level identifiers (see image below). So, the Enum statement is only valid when written outside all the procedures in a module
- You can include either the Private or Public keyword in the Enum statement, but not both. Regardless of your choice, you must always include the Enum keyword.
- Enums have public scope by default, unlike constants which are private by default. So, the Enum and Public keywords produce the same effect. However, including the Public keyword improves code readability.
- You can omit an Enum member’s (i.e., any constName in the syntax above) value assignment. In that case, the compiler assigns a number (of Long data type) to the member. Also, the compiler assigns zero (0) to the member if the member is the first in the Enum. Otherwise, the compiler assigns a number greater than the preceding Enum member’s value by one (1).
- Once you’ve declared an Enum, you can declare other program identifiers with the Enum’s name as their type. These other identifiers that this applies to include variables, arguments, or function returns. Now, these variables, arguments, or function returns can only contain the values mapped to them in the Enum’s declaration.
The image below shows the different scope levels in VBA. Also, we’ve delved into scope levels in a separate article, make sure to check it out if you need a refresher.
Sample Code (Examples)
The sample code below shows the Enum statement in use. Also, it illustrates the salient points discussed above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Option Explicit 'declare enums accessible only to procedures in the current module Private Enum enu_NamesCategory African 'assigned a default value of '0' English 'assigned a default value of '1' Chinese = 10 'other members are given values starting from '1' Indian 'assigned a default value of '11' End Enum 'declare enums accessible to procedures in/out-side the current module Public Enum enu_SelectionCriteria Education = 0 Experience = 1 Certification = 2 End Enum ' adding PUBLIC is more readable than using just ENUM Sub Enum_Statement_Procedure_Level_Examples() 'declare a procedure-level variable with Enum type Dim lng_NamesOrigin As enu_NamesCategory 'see enum members' values in the Immediate Window 'African lng_NamesOrigin = African Debug.Print "African's value is " + Str(lng_NamesOrigin) 'English lng_NamesOrigin = English Debug.Print "English's value is " + Str(lng_NamesOrigin) 'Indian lng_NamesOrigin = Indian Debug.Print "Indian's value is " + Str(lng_NamesOrigin) 'declare a procedure-level variable with Enum type Dim lng_SuccessPredictor As enu_SelectionCriteria ' variable can only hold values assigned in enum declaration End Sub |
When you run the sample code above, the Str function (lines #27, #30, and #33) returns the String representation of the Enum (which is of Long data type) passed to it as an argument. Those function return-values are displayed in the Immediate window using the Print method of the Debug object, as shown below.