Professional VBA development is about more than just making code work; it’s about making it readable and maintainable. The VBA Enum statement is a powerful feature that allows you to create custom sets of related constants. By using Enums, you replace “magic numbers” (confusing numeric values) with descriptive names, significantly improving your Excel VBA programming workflow.
The Power of the VBA Enum Statement
The primary role of a vba custom enumeration is to assign meaningful names to integral values. Instead of remembering that “3” stands for “Red,” you simply use ColorEnum.Red. This not only makes your code self-documenting but also activates VBA IntelliSense, which provides a dropdown list of options as you type.
Syntax of the VBA Enum Statement
The Enum statement must be declared at the top of a module, in the Declarations section. It cannot be declared inside a Sub or Function.
[Public | Private] Enum enumname
membername [= constantexpression]
membername [= constantexpression]
...
End Enum
Key Components:
- Scope (Public/Private):
Public(default) makes the Enum available to the whole project;Privatelimits it to the current module. - EnumName: The unique name for your collection of constants.
- Automatic Numbering: If you don’t assign a value, VBA starts at 0 and increments by 1 for each subsequent member.
Top 5 Examples of VBA Enums in Action
Example 1: Mapping Days of the Week
This is the classic use case for an excel vba enumeration. It replaces obscure day numbers with clear names.
Public Enum WeekDays
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
End Enum
Example 2: Driving ‘Select Case’ with IntelliSense
Using an Enum in a Select Case block makes your logic incredibly easy to follow. Notice how we declare the variable as the Enum type itself.
Sub CheckSchedule()
Dim currentDay As WeekDays
currentDay = WeekDays.Monday
Select Case currentDay
Case WeekDays.Monday: MsgBox "Back to work!"
Case WeekDays.Saturday, WeekDays.Sunday: MsgBox "Weekend vibes!"
End Select
End Sub
Example 3: Defining UI Theme Colors
Instead of memorizing ColorIndex numbers, create a ColorEnum for your project’s brand colors.
Enum BrandColors
PrimaryRed = 3
SuccessGreen = 4
AccentBlue = 5
End Enum
Example 4: Iterating Through Enum Values
While you can’t “loop” through an Enum directly like a collection, you can iterate through the underlying long integers. This is useful for populating dropdowns or validating inputs.
Sub LoopThroughDays()
Dim i As Long
' Loop from the first member to the last
For i = WeekDays.Monday To WeekDays.Sunday
Debug.Print "Day Number: " & i
Next i
End Sub
Example 5: Custom Error Codes for Error Handling
Standardize your VBA error handling by assigning custom codes to specific failure scenarios.
Enum AppErrors
FileNotFound = 5001
DatabaseConnectionFailed = 5002
InvalidUserInput = 5003
End Enum
Important Notes & Best Practices
- IntelliSense Advantage: When you declare a variable as your
Enumtype, VBA will show a list of all members whenever you type an equals sign (=). - Storage: Enums are stored internally as Long Integers.
- No Strings: Enum members can only be integers. You cannot assign a string (e.g.,
Monday = "Mon") to an Enum. - Uniqueness: Each member name must be unique within the module where the Enum is declared.
Conclusion
The VBA Enum statement is an essential tool for creating clean, professional VBA code. By leveraging enumerations, you reduce bugs caused by “magic numbers” and make your macros much easier for others (or your future self) to understand. Start using Enums in your Analysistabs project management tools today!



I think that Example # 4 is wishful thinking. I wish it were true, but an Enum is not a Collection or Array object.
Hi Eric,
I wanted to personally thank you for your comment on the VBA Enums article. You are 100% right—Example #4 was incorrect, as an Enum cannot be iterated with a For Each loop.
That’s a great catch, and I’m genuinely grateful you took the time to highlight it. Feedback like yours is invaluable for maintaining accurate and helpful content.
I have now corrected the example to show the proper technique using a For…Next loop that iterates from the first to the last member’s value.
Thanks again for your contribution!
Best regards,
PNRao
Example # 2 code returned an compile error for me: Method or data member not found at the line Day = WeekDays.Monday. Am I possibly missing a reference?
Hi! Thank you for bringing this up. You actually don’t need a special library reference for Enums, as they are a native feature of VBA. That “Method or data member not found” error is almost always caused by where the code is placed.
In VBA, Enums must be declared in the “Declarations” section at the very top of your module. If you place the
Enuminside aSuborFunction, VBA cannot recognize it as a data type, which triggers the compile error.Here is the full, corrected code for Example 2. Please ensure the
Enumblock is at the top of a standard module:' --- PLACE THIS AT THE VERY TOP OF YOUR MODULE --- Public Enum WeekDays Monday = 1 Tuesday = 2 Wednesday = 3 Thursday = 4 Friday = 5 Saturday = 6 Sunday = 7 End Enum ' --- THEN PLACE YOUR PROCEDURE BELOW --- Sub DisplayMessage() Dim Day As WeekDays ' Set Day to be Monday (IntelliSense should now show a list here) Day = WeekDays.Monday Select Case Day Case WeekDays.Monday MsgBox "Today is Monday." Case WeekDays.Tuesday MsgBox "Today is Tuesday." Case WeekDays.Wednesday MsgBox "Today is Wednesday." Case WeekDays.Thursday MsgBox "Today is Thursday." Case WeekDays.Friday MsgBox "Today is Friday." Case WeekDays.Saturday MsgBox "Today is Saturday." Case WeekDays.Sunday MsgBox "Today is Sunday." End Select End SubQuick Check: If your
DisplayMessagecode is in a different module than yourEnum, make sure the Enum is declared as Public. If it’s Private, other modules won’t be able to “see” it.Give that a try and let me know if it clears the error!
Best regards,
PNRao