Enumerations

Any built-in constants are examples of symbolic constants and generally start with the characters vb.
An enumeration is a type that has been defined to represent a fixed set of possible values.
These are basically constants that you can use directly in your code.
Each value corresponds to an integer value.
When you use an enumeration you must specify the enumeration type as well as the enumeration value.
An enumeration is a distinct value which provides an alternative to using a constant.
Every enumeration has an underlying type which can be any integral type: Integer, Short, Long etc
Enumeration are typically in groups and provide a way of categorising your symbolic constants into a defined structure.
A list of built-in enumerations can be seen in the object browser
Do user defined enumerations appear in the object browser ?


 


Built-in Enumerations

There are a huge number of built-in enumerations that are used to categorise all the built-in constants.
It is often a lot easier to remember the symbolic constant names rather than the actual numeric values.
Using enumerations has the added benefit of enablying the Auto List Members feature to provide help.
A good example of a built-in enumeration is related to the MsgBox function.

Public Enum vbMsgBoxResult 
   vbOK = 1
   vbCancel = 2
   vbAbort = 3
   vbRetry = 4
   vbIgnore = 5
   vbYes = 6
   vbNo = 7
End Enum


Displaying the Enumeration Label



Displaying the Enumeration Value

When you want to display the value of an enumerated constant rather than its name you must cast the constant to its underlying type
For example:

  CType(enTemperatire.Constant2, Integer) 



Public Enum enEnumeration 
   MyFirstValue = 1 '0 is the default, can be overwritten to any value
   MySecondValue
   MyNamedRange1
   MyFourthValue
End Enum

Public Sub Testing()
Dim svalue As String
' Debug.Print [MyNamedRange1].Address 'this will not compile with an 'Invalid Qualifier' error
' Debug.Print [MyNamedRange2].Address
    
' svalue = Application.VLookup(20, [MyNamedRange2], 2, False)
' Debug.Print svalue ' = two
    
'this will compile but will generate a 'Type Mismatch' when actually run
'This is becuase the enumeration is beinf used first
' svalue = Application.VLookup(20, [MyNamedRange1], 1, False)
    
'this can be tested by running the following line
'if the named range was being used this would generate a 'Type Mismatch' error
    Debug.Print [MyNamedRange1] ' = 3
End Sub


Important

When you are referring to named ranges in your code never use the Range abbreviation [MyNamedRange].
Always fully qualify your enumerations.
Always prefix your enumeration values with a "en" prefix so they are not accidentally confused with named ranges.
These are not available in Office 97 and were only introduced in Office 2000.


No Prefixes

Not all the enumerations have the mso prefix


© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Next