An enumeration is a collection of Long (Integer) data types that have fixed numeric values.
The underlying data type cannot be explicitly defined because all enumerations are defined with a Long data type.
Enumerations provide a way of categorising (or grouping) your symbolic constants into a defined structure.
Instead of these 3 individual lines of code:
Public Const lValue1 As Long = 0
Public Const lValue2 As Long = 1
Public Const lValue3 As Long = 2
You can group these constants as a single enumeration.
Public Enum enTypeName
lValue1 = 0
lValue2 = 1
lValue3 = 2
When you use an enumeration value in your code you should always specify the enumeration type as well.
These 2 lines of code all work, but the first one is the preferred syntax.
Call MsgBox(enTypeName.lValue2) ' always prefix the value with the enumeraton type
Call MsgBox(lValue1) ' the enumeration type is missing
A lot of the built-in constants are grouped into enumerations which typically start with the characters vb.
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.
There is a built-in enumeration called vbMsgBoxResult which can be used.
This built-in enumeration has been defined in the following way:
Public Enum vbMsgBoxResult
vbOK = 1
vbCancel = 2
vbAbort = 3
vbRetry = 4
vbIgnore = 5
vbYes = 6
vbNo = 7
These 3 lines of code all work, but the first one is the preferred syntax.
Call MsgBox(vbCancel) ' the enumeration type is missing
Call MsgBox(VBA.vbMsgBoxResult.vbCancel) ' the VBA prefix is excessive
Built-in Enumerations - Complete List
A list of built-in enumerations can be seen in the object browser
Excel Enumerations - Complete List
Here is the Complete List of all the Excel enumerations.
Word Enumerations - Complete List
Here is the Complete List of all the Word enumerations.
PowerPoint Enumerations - Complete List
Here is the [[Complete List]] of all the PowerPoint enumerations.
Not all the built-in enumerations have the "vb" or "mso" prefix.
Do user defined enumerations appear in the object browser ?
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopNext