In a Class Module there is:
Private Enum colType
ID = "A"
SSN = "B"
lName = "H"
fName = "G"
End Enum
as a private member. Whenever the class initializes I get the Compile Error: Type Mismatch message. If I declare colType
as Private Enum coltype As String
. That gets highlighted red as an error and I get the message:
Compile Error: Expected end of statement
Is specifying the values of enumerated elements Unallowed in Excel VBA?
Excel VBA Enum – Example #1Step 1: In the developer's tab click on Visual Basic to open VB Editor. Step 2: Insert a new module from the insert tab in the VB Editor. Step 3: Declare the Enum function with an enum name. Step 5: Now declare a sub-function below the enum function to use the enum.
In Excel, VBA Enum is a type that contains an enumeration of constants. Enumerations create a list of items and make them in a group. For example, type of mobiles: “Redmi, Samsung, Apple, Vivo, Oppo.” Using enumerations, we can group together all of them under a single value.
In this situation, one can declare the enumerated type in which only male and female values are assigned. It can be declared during declaring enumerated types, just add the name of the variable before the semicolon. or, Beside this, we can create enumerated type variables as same as the normal variables.
As written in the comments, this is not possible. There is possible workaround though that I used in the past. Have:
Private Enum colType
ID = 1
SSN = 2
lName = 3
fName = 4
End Enum
And then create a separate String property of function such as:
Public Property Get colType_String(colType) as String
Dim v as Variant
v= Array("A","B", ...)
colType_String = vba.cstr(v(colType))
End Property
This is not the most universal solution, but it is easy to implement and it does the job... If you have this in the class module already you can even use property on private colType variable and there is no need to have colType input into the property.
I quite like ex-man's solution in certain circumstances, for which reason I've upvoted it. The solution more often posited goes along the following lines:
Enum myEnum
myName1 = 1
myName2 = 2
myName3 = 3
End Enum
Function getEnumName(eValue As myEnum)
Select Case eValue
Case 1
getEnumName = "myName1"
Case 2
getEnumName = "myName2"
Case 3
getEnumName = "myName3"
End Select
End Function
Debug.Print getEnumName(2) prints "myName2"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With