An enumeration is a collection of Long/Integer data types that have fixed numeric values.
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 enValue1 As Integer = 0
Public Const enValue2 As Integer = 1
Public Const enValue3 As Integer = 2
You can group these constants as a single enumeration.
Public Enum enTypeName
enValue1 = 0
enValue2 = 1
enValue3 = 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(enValue1) ' 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
Not all the built-in enumerations have the "vb" or "mso" prefix.
Do user defined enumerations appear in the object browser ?
© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopNext