Results 1 to 1 of 1

You might need Enum values explained - try this

Threaded View

  1. #1
    Forum Contributor
    Join Date
    10-18-2012
    Location
    Telford, England
    MS-Off Ver
    Excel 2010 (2003)
    Posts
    294

    You might need Enum values explained - try this

    Yesterday i was a-Googling (there are other search engines) and came across a very useful page; http://www.datapigtechnologies.com/d...umerations.txt
    that lists MS enumerations, e.g. (a short one):
    Public Enum XlApplyNamesOrder
    xlColumnThenRow = 2
    xlRowThenColumn = 1
    End Enum
    Now one can readily get the value usually hidden by a name. But what if you want the name for a value as I do in my reporter tools? Easy! Place the data in a module (name it XLEnumertions), add a macro to fetch the result and it's done. Here's the code:
    '===============================================
    ' Find the reserved word naming the value passed
    '===============================================
    '
    Function DeEnumerateMS(myEnum As String, myValue As Integer, myName As String) As Boolean
    ' Requires: Enum name, value. Returns reserved name (myName does not need to be initialised before call)
    ' Return true and reserved name if found, return False if not found
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Dim SL As Long ' start line
    Dim SL2 As Long ' end enum line
    Dim EL As Long ' end line
    Dim SC As Long ' start column
    Dim EC As Long ' end column
    Dim S As String, E As String
        
        Set VBProj = ThisWorkbook.VBProject         ' I'm reading my own source!
        Set VBComp = VBProj.VBComponents("XLEnumertions")  ' this module name
        Set CodeMod = VBComp.CodeModule
            
        With CodeMod
            SL = 4                                  ' start at line 4 after the credits
            EL = .CountOfLines                      ' search initially the whole module
            SC = 1
            EC = 15
            
        ' find the Enum name
            If Not .Find(Target:=myEnum, _
                StartLine:=SL, _
                EndLine:=CodeMod.CountOfLines, _
                StartColumn:=13, _
                EndColumn:=EC, _
                wholeword:=True, MatchCase:=False, patternsearch:=False) Then
                
                myName = "Enum"                     ' did not finde Enum specified
                Exit Function
            End If
        ' locate the End Enum line (must be one)
            SL = SL + 1
            SL2 = SL
            B = .Find(Target:="End Enum", _
                StartLine:=SL2, _
                EndLine:=CodeMod.CountOfLines, _
                StartColumn:=1, _
                EndColumn:=8, _
                wholeword:=True, MatchCase:=False, patternsearch:=False)
            
        ' locate the value within the Enum bounds just identified
            E = "= " & CStr(myValue)
            For SL = SL To SL2
                S = CodeMod.Lines(SL, 1)
                x = InStrRev(S, E)
                If x > 0 Then
                ' return success directly
                    myName = Mid(S, 5, x - 6)
                    DeEnumerateMS = True
                    Exit Function
                End If
            Next
        ' not found so fail on enum value
            myName = "Value"                        ' Enum OK but value not found
        
        End With
    End Function
    I'm indebted to http://www.cpearson.com/Excel/VBE.aspx for the method in the code.

    This macro assumes these things:
    1. You've reduce the headings at teh top of the module from Datapig to fewer than 4 lines (put comments at the foot of the module after the functin).
    2. 'Trust access to the VBA Project Object model' must be true to allow reading the source directly.
    3. VBA extensibility 5.3 must be referenced in Tools
    Then place the module in your personal workbook, or perhaps an add-in. It is then available to other macros you write. Add project references if needed.
    One odd thing is that the code does read its own module code to get the definitions, thereby not duplicating that data anywhere.
    Last edited by brynbaker; 08-09-2013 at 06:46 AM. Reason: furthe info

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Enum: use same user-defined name in 2 Enum's
    By onidarbe in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-11-2013, 11:25 AM
  2. Not sure if its explained well, but hope it can be done....
    By Joe O Ceadaigh in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-29-2012, 01:50 PM
  3. need help with a v lookup formula, just need it to be explained
    By damagedbodies in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-12-2010, 01:08 PM
  4. DLL's explained?
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2008, 08:28 AM
  5. Counting Formulas -- Re-explained
    By MAB in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-12-2006, 05:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1