MACRO LIBRARY

Enumeration example

Free public access. No account or subscription required.

Files

Enumeration example

Code

'Enumerated values
'http://www.cpearson.com/excel/Enums.aspx
    
'This is an acceptable way to create constants:
    
Const C_APPLE = 1
Const C_ORANGE = 2
Const C_PLUM = 3
    
'The problem is that you must always know the names
'Instead lets use enum so we can make these constant values available via intellisense
    
Enum FruitType
    Apple = 1
    Orange = 2
    Plum = 3
End Enum
    
Sub main()
    'Using the first method we would have needed this to set Fruit equal to the apple value
    'Dim Fruit As Long
    'Fruit = C_APPLE
    
    'but with enums we can do the following
    Dim Fruit As FruitType
    Fruit = Apple
    
    Debug.Print Fruit
End Sub