The Enum Statement | VBA Declaration Statements

Declaring Enumerations - the Enum statement’s syntax. Square brackets, [ ], indicate optional items while vertical bars, |, indicate mutually exclusive items.
Declaring Enumerations - the Enum statement’s syntax. Square brackets, [ ], indicate optional items while vertical bars, |, indicate mutually exclusive items.
The Enum statement declares VBA enumerations or Enums. Enums are widely used in VBA programs, so it's useful exploring their declaration, as you'll do here.

In this article:

3 minutes read

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.

VBA Program Identifiers – Scope Levels [from the narrowest (block level) to broadest (application level)]
VBA Program Identifier Scope Levels – from the narrowest (block-level) to broadest (application level)

Sample Code (Examples)

The sample code below shows the Enum statement in use. Also, it illustrates the salient points discussed above.

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.

Sample code illustrating the Enum statement’s usage.
Sample code illustrating the Enum statement’s usage.
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Keep Learning

Related Articles

Keep Learning

Related Articles