Is there a way to count elements in a VBA enum?

后端 未结 5 1999
无人及你
无人及你 2021-02-09 14:29

is there a proper way to count elements of an enum in VBA ? At the moment, I leave an enum value such as KeepThisOneHere in the following example

En         


        
相关标签:
5条回答
  • 2021-02-09 14:48

    Here's an example of my workaround, which is pretty straightforward:

    Enum FileSpecFields
        FileSpecFields_Start                    '(zero-based)
            FileNameIdx = FileSpecFields_Start
            FolderNameIdx
            BasePathIdx
            FullPathIdx
            CopyStatus
        FileSpecFields_End = CopyStatus
    End Enum
    
    '...
    
    ReDim FileSpecList(1 To MaxFiles, FileSpecFields_Start To FileSpecFields_End) As String
    
    '...
    

    But note that, if you are using a one-based Enum you may have to adjust the _End value definition, depending on how you're using it. Also, for zero-based Enums, the _End value is not the same as its count of items. And, if you add items at the end, you must update the _End value's definition accordingly. Finally, if your enum is a non-contiguous range of values, all bets are off with this approach!

    0 讨论(0)
  • 2021-02-09 14:57

    Not sure on the etiquette here, so I'll post it and if advised, I'll come back and delete it. Chip Pearson posted this code on the Code Cage Forums (http://www.thecodecage.com/forumz/microsoft-excel-forum/170961-loop-enumeration-constants.html). I don't have the TypeLinInfo DLL on my machine, so I can't test it (I'm sure google will turn up places to download TLBINF32.dll). Nonetheless, here is his entire post to save someone else from registering for a forum:

    You can do this ONLY IF you have the TypeLibInfo DLL installed on your computer. In VBA, go to the Tools menu, choose References, and scroll down to "TypeLib Info". If this item exists, check it. If it does not exist, then quit reading because you can't do what you want to do. The file name of the DLL you need is TLBINF32.dll.

    The following code shows how to get the names and values in the XLYesNoGuess enum:

    Sub AAA()
        Dim TLIApp As TLI.TLIApplication
        Dim TLILibInfo As TLI.TypeLibInfo
        Dim MemInfo As TLI.MemberInfo
        Dim N As Long
        Dim S As String
        Dim ConstName As String
    
        Set TLIApp = New TLI.TLIApplication
        Set TLILibInfo = New TLI.TypeLibInfo
        Set TLILibInfo = TLIApp.TypeLibInfoFromFile( _
            ThisWorkbook.VBProject.References("EXCEL").FullPath)
    
        ConstName = "XLYesNoGuess"
        For Each MemInfo In _
            TLILibInfo.Constants.NamedItem(ConstName).Members
            S = MemInfo.Name
            N = MemInfo.Value
            Debug.Print S, CStr(N)
        Next MemInfo
    End Sub
    

    Using this knowledge, you can create two useful functions. EnumNames returns an array of strings containing the names of the values in an enum:

    Function EnumNames(EnumGroupName As String) As String()
        Dim TLIApp As TLI.TLIApplication
        Dim TLILibInfo As TLI.TypeLibInfo
        Dim MemInfo As TLI.MemberInfo
        Dim Arr() As String
        Dim Ndx As Long
        Set TLIApp = New TLI.TLIApplication
        Set TLILibInfo = New TLI.TypeLibInfo
        Set TLILibInfo = TLIApp.TypeLibInfoFromFile( _
            ThisWorkbook.VBProject.References("EXCEL").FullPath)
        On Error Resume Next
        With TLILibInfo.Constants.NamedItem(EnumGroupName)
            ReDim Arr(1 To .Members.Count)
            For Each MemInfo In .Members
                Ndx = Ndx + 1
                Arr(Ndx) = MemInfo.Name
            Next MemInfo
        End With
    
        EnumNames = Arr
    End Function
    

    You would call this function with code such as:

    Sub ZZZ()
        Dim Arr() As String
        Dim N As Long
        Arr = EnumNames("XLYesNoGuess")
        For N = LBound(Arr) To UBound(Arr)
            Debug.Print Arr(N)
        Next N
    End Sub
    

    You can also create a function to test if a value is defined for an enum:

    Function IsValidValue(EnumGroupName As String, Value As Long) As
        Boolean
        Dim TLIApp As TLI.TLIApplication
        Dim TLILibInfo As TLI.TypeLibInfo
        Dim MemInfo As TLI.MemberInfo
        Dim Ndx As Long
        Set TLIApp = New TLI.TLIApplication
        Set TLILibInfo = New TLI.TypeLibInfo
        Set TLILibInfo = TLIApp.TypeLibInfoFromFile( _
            ThisWorkbook.VBProject.References("EXCEL").FullPath)
        On Error Resume Next
        With TLILibInfo.Constants.NamedItem(EnumGroupName)
            For Ndx = 1 To .Members.Count
                If .Members(Ndx).Value = Value Then
                    IsValidValue = True
                    Exit Function
                End If
            Next Ndx
        End With
        IsValidValue = False
    End Function
    

    This function returns True if Value is defined for EnumGroupName or False if it is not defined. You would call this function with code like the following:

    Sub ABC()
        Dim B As Boolean
        B = IsValidValue("XLYesNoGuess", xlYes)
        Debug.Print B ' True for xlYes
        B = IsValidValue("XLYesNoGuess", 12345)
        Debug.Print B ' False for 12345
    End Sub
    

    Cordially, Chip Pearson Microsoft MVP 1998 - 2010 Pearson Software Consulting, LLC www.cpearson.com [email on web site]

    0 讨论(0)
  • 2021-02-09 14:59
    Sub count()
        Dim n, c
    
        For n = headers.frstItem To headers.lastItem
            c = c + 1
        Next
        Debug.Print c
    End Sub
    
    0 讨论(0)
  • 2021-02-09 15:06

    If you know the enum type(s) on design-time you could transform them into a Static Property Get MyEnumColl() as Collection ... (no class needed, initialized on 1st access statically) and thus easily loop through them or count them like shown here

    0 讨论(0)
  • 2021-02-09 15:09

    there isn't a way to get the count.

    What you have to do is loop through the elements of the Enum until you get to the last one.

    Chip Pearson has some good tips on Enumerated constants: Chip Pearson: Enum Variable Type

    0 讨论(0)
提交回复
热议问题