Get Enum string Value

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows
below code have compile Error: GetType(flavorEnum)
Code:
Public Enum flavorEnum
  salty
  sweet
  sour
  bitter
End Enum


Private Sub TestMethod()
  MsgBox ("The strings in the flavorEnum are:")
  Dim i As String
  For Each i In [Enum].GetNames(GetType(flavorEnum))
      MsgBox (i)
  Next
End Sub
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Good day Mr is there any way to get enum string value

Thanks
 
Upvote 0
Good Day
I get Enum String Values
and this may need your touch need a Professional

Code:
Enum SecurityLevel
IllegalEntry
    SecurityLevel1
    SecurityLevel2
End Enum


Public Sub Test()
Dim E As Long
Call AddReferenceVBA
For E = SecurityLevel.IllegalEntry To SecurityLevel.SecurityLevel2
MsgBox StrEnumVal("SecurityLevel", E)


Next
End Sub


Public Function StrEnumVal(EnumName As String, EnumItm As Long) As String
' Reference  "Microsoft Visual Basic for Applications Extensibility 5.3" must be installed
'Call AddReferenceVBA
On Error Resume Next
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim CodeMac As VBIDE.CodePane
Dim numLines As Long ' end line
Dim lineNum As Long
Dim thisLine As String
Dim message As String
Dim numFound As Long
Dim IsEnum As Boolean
Dim Itm As Long


Set VBProj = ThisWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Module1")
Set CodeMod = VBComp.CodeModule
       With CodeMod
           numLines = .CountOfLines
            Fnd = "Enum " & Fnd
            Itm = numLines
           For lineNum = 1 To numLines
               thisLine = .Lines(lineNum, 1)
              
               If InStr(1, thisLine, Fnd, vbTextCompare) > 0 Then
               IsEnum = True
               End If
               If IsEnum = True Then
               
                   If InStr(1, thisLine, "End Enum", vbTextCompare) > 0 Then Exit For
                  If InStr(thisLine, ":") > 0 Then
                  For s = 0 To UBound(Split(thisLine, ": "))
                   EnumItems = EnumItems & IIf(EnumItems <> "", vbNewLine, "") & Split(Split(thisLine, ": ")(s), " = ")(0)
                   Next
                   Else
                   EnumItems = EnumItems & IIf(EnumItems <> "", vbNewLine, "") & Split(thisLine, " = ")(0)
                   Itm = Itm + 1
                   End If
                   
               End If
           Next lineNum
       End With
       If InStr(EnumItems, Fnd & vbNewLine) > 0 Then
        EnumItems = Replace(EnumItems, Fnd & vbNewLine, "")
       Else
        EnumItems = Replace(EnumItems, Fnd, "")
       End If
       Fnd = ""
       EnumItems = Replace(EnumItems, " ", "")
        StrEnumVal = Split(EnumItems, vbNewLine)(EnumItm)
End Function
Sub AddReferenceVBA()
    AddRef ThisWorkbook, "{0002E157-0000-0000-C000-000000000046}", "VBIDE", 5, 3
End Sub


Sub AddRef(wbk As Workbook, sGuid As String, sRefName As String, sRefMajor As Long, sRefMinor As Long)
    Dim i As Integer
    On Error GoTo EH
    With wbk.VBProject.References
        For i = 1 To .Count
            If .Item(i).Name = sRefName Then
             
               Exit For
            End If
        Next i
        If i > .Count Then
        
           .AddFromGuid sGuid, sRefMajor, sRefMinor ' 0,0 should pick the latest version installed on the computer
            ThisWorkbook.Save
        End If
    End With
EX: Exit Sub
EH: MsgBox "Error in 'AddRef'" & vbCrLf & vbCrLf & Err.Description
    Resume EX
    Resume ' debug code
End Sub
 
Last edited:
Upvote 0
it is Working but may need your comments

and This Note: Set VBComp = VBProj.VBComponents("Module1") to who use it shoud be the name of Module1 inserted.

thanks
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top