In Excel, an “enum” is not a built-in feature—it is a concept from VBA (Visual Basic for Applications) where a named constant represents an integer value. In a worksheet, the closest thing is a defined name or a data validation list. This article explains what enums are in Excel, how to use them in VBA, and how to handle common issues when working with enumerated values.
What Is an Enum in Excel?
An enum (short for enumeration) is a programming construct that assigns human-readable names to a set of integer values. In Excel’s VBA, you declare an enum with the Enum keyword. For example:
Enum Priority
Low = 1
Medium = 5
High = 10
End Enum
This creates a type named Priority with three named constants. In your code, you can use Priority.High instead of the number 10, making your code more readable and less error-prone. Enums are stored as Long (32-bit integer) values by default.
In the worksheet itself, there is no native “enum” feature. However, you can simulate enums using defined names (e.g., Low=1, Medium=5, High=10) or by using a data validation dropdown that maps text to numbers with formulas like VLOOKUP.
How to Use Enums in VBA
To use an enum in VBA, you declare it at the top of a module (outside any procedure). Then you can declare variables of that enum type and assign values:
Dim p As Priority
p = Priority.Medium
MsgBox p ' Displays 5
Enums are particularly useful in user-defined functions (UDFs) or when writing macros that need to handle a fixed set of options. For example, a function that returns the priority level based on a number:
Function GetPriorityName(num As Long) As String
Select Case num
Case Priority.Low: GetPriorityName = "Low"
Case Priority.Medium: GetPriorityName = "Medium"
Case Priority.High: GetPriorityName = "High"
Case Else: GetPriorityName = "Unknown"
End Select
End Function
Always save a copy of your workbook before running a macro that uses enums, because macros cannot be undone with Ctrl+Z.
Worksheet Simulation of Enums
If you want to use enum-like behavior in a regular worksheet (without VBA), you can create a named range for each constant. For instance, define a name Low that refers to =1, Medium to =5, and High to =10. Then in a formula you can write =IF(A1=Medium, "OK", "Check").
Another approach is to use a lookup table. Suppose you have a table with values in A2:A4 (1, 5, 10) and labels in B2:B4 (Low, Medium, High). You can use VLOOKUP to convert a number to its label:
=VLOOKUP(D2, $A$2:$B$4, 2, FALSE)
This returns “Medium” if D2 contains 5. For the reverse (label to number), use INDEX and MATCH:
=INDEX($A$2:$A$4, MATCH(E2, $B$2:$B$4, 0))
These methods give you the readability of enums without any VBA.
Common Problems and Fixes
One frequent issue is that enums in VBA are not available in the worksheet directly. If you try to use Priority.High in a cell formula, Excel will return a #NAME? error. To fix this, use the underlying numeric value or a defined name.
Another problem occurs when you change an enum’s values after you’ve already used them in saved data. For example, if you stored 5 in a cell and later change Medium to 6, your old data no longer matches. Always keep enum values stable or use a versioning system.
When using Enum in VBA, you might accidentally assign a value outside the defined set. VBA does not enforce that a variable of an enum type only contains valid values; it can hold any Long. Always validate inputs.
Choosing Between Enum and Other Methods
Deciding whether to use a VBA enum, a defined name, or a lookup table depends on your needs:
- VBA enum: Best for code that runs in macros or UDFs. It makes code self-documenting and reduces magic numbers.
- Defined name: Good for simple constants you use in formulas. For example, define
TaxRateas=0.08and use it in formulas. - Lookup table: Ideal when you need to map many codes to labels, or when the mapping might change. It is easy to update without editing formulas.
If you need to share your workbook with others who might not understand VBA, a lookup table is often more transparent. If you are writing complex automation, an enum is cleaner.
Enums in Other Office Applications
Enums are also used in other Microsoft Office applications. For example, in Word VBA, you have WdSaveFormat to specify file formats, and in PowerPoint, PpSaveAsFileType. These are built-in enums that you can use without declaring them. In Excel, built-in enums include XlFileFormat (for file types) and XlDirection (for navigation). You can see a list of built-in enums in the Object Browser (press F2 in the VBA editor).
When using built-in enums, you often see numeric values in recorded macros. For example, ActiveWorkbook.SaveAs Filename:="file.xlsx", FileFormat:=51 where 51 is xlOpenXMLWorkbook. Using the named constant is better because it is self-explanatory.
Frequently Asked Questions
Can I use an enum in a cell formula? No, enums are a VBA concept. In a worksheet, use defined names or a lookup table to achieve similar readability.
How do I find the numeric value of a built-in enum? In the VBA editor, press F2 to open the Object Browser, search for the enum name (e.g., XlFileFormat), and click it. The members and their values appear in the lower pane.
Why does my enum not appear in the IntelliSense list? Enums must be declared at the top of a standard module (not inside a procedure) to be visible throughout the project. If it is in a class module, you may need to qualify it with the class name.
Can I use enums in user-defined functions (UDFs) called from the worksheet? Yes, but the function will return the numeric value unless you convert it to text. To return a label, use a Select Case or a lookup table inside the function.
Is there a way to create an enum in Excel without VBA? Not natively. The closest is using defined names or a validation list with a lookup formula. For example, you can create a data validation list that shows “Low, Medium, High” and then use MATCH to get the number.
Best Practices and Alternatives
When using enums in VBA, follow these best practices:
- Use explicit values (e.g.,
Low = 1) rather than relying on automatic increments, to avoid breaking existing data. - Prefix enum type names with a short identifier (e.g.,
prioLow) to avoid naming conflicts with other constants. - Document the meaning of each value in comments.
If you need to store enum values in a worksheet and later use them in VBA, consider using the numeric value and converting with a function. For example, EnumToLabel as shown earlier. This keeps your data stable even if you reorder the enum.
For users who prefer not to use VBA at all, the lookup table approach is the most flexible and maintainable. It also works in Excel for the web and on Mac, whereas VBA is not available in Excel for the web and has limited support on Mac (though it does work).
In summary, Excel does not have a direct “enum value” feature in the worksheet, but you can achieve the same effect with VBA enums, defined names, or lookup tables. Choose the method that fits your skill level and the complexity of your task.
Related
- How to Add Subtotals in Google Sheets
- How to Add Value to a Dictionary in Python
- Import Excel CSV to InfluxDB GUI: Step-by-Step
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Errors & fixes
- Excel Row Won’t Expand? Fix Height and Hidden RowsIf an Excel row won’t expand, select it and choose Home > Format > AutoFit Row Height, or…
- Google Sheets Keeps Changing Date Format? Here’s WhyGoogle Sheets usually changes a date because it automatically recognizes typed or pasted text as a date, or…
- Excel Power Query Stop Auto Refresh? Fix ItYes, you can stop Power Query from auto-refreshing in Excel by disabling background refresh in the query’s properties,…
- Excel Conditional Formatting Duplicate Rules: How to FixTo fix duplicate conditional formatting rules in Excel, select the range, go to Home > Conditional Formatting >…
- Excel Filter Not Working After a Certain Row? Fix ItIf Excel filtering stops at a certain row, the filter range usually ends there—often because a completely blank…
- Excel Can’t Save Here Error: Causes, Fixes, and ChecksThe Excel message “You can’t save here” usually means Excel cannot write the workbook to the selected location,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.