Consider this statement:
MsgBox “Hello!”, vbCritical + vbYesNo
vbCritical and vbYesNo are members of a built-in enumeration called vbMsgBoxStyle. You can create your own enumerations containing constants of your choice for use in your applications. To do so, you use the Enum statement at the module level.
dkHello = 1
dkWorld = 2
dkExclaim = 4
In the above example, the members are assigned specific values, but those values are optional. If you omit the values, they are automatically assigned consecutive long integers starting with zero (0,1,2…).
Enumerations can be additive or non-additive. The enumeration for a MsgBox style is additive, meaning that you can add members together. There’s nothing special in the Enum statement that identifies additive enumerations, it’s the code that accepts the enumeration that handles the additive nature. For built-in, additive enumerations, the values assigned to the members are usually 1, 2, 4, 8, 16, etc. so you can add two or more together and get a unique number.
Here’s a sub that accepts the above enumeration to create a message box.
Sub SayHello(eText As dkMsgText)
Dim sText As String
Select Case eText
sText = “Hello “
sText = “World “
sText = “Hello World “
sText = “!”
sText = “Hello !”
sText = “World !”
sText = “Hello World !”
SayHello dkHello + dkWorld
Testing the additive nature of the enumeration can be done without testing every possible combination by using bitwise comparisons. But that’s a subject for another post.