获取枚举的文本等效项

时间:2018-01-25 18:07:00

标签: excel vba excel-vba enumeration

我正在尝试在工作表中创建一些简单的表,这些表提供了常见枚举的数字等效项:

enter image description here

此示例适用于Border枚举变量。这种材料可以在MSDN中闲逛找到,但是当我不在线时,我经常需要工作,这种类型的"帮助"不可用。

我目前正在用两个独立的循环填充我的小桌子:

Sub trythisB()
    Dim i As Long
    i = 1
    For Each a In Array(xlInsideHorizontal, xlInsideVertical, xlEdgeLeft, xlEdgeRight, xlEdgeBottom, xlEdgeTop)
            Cells(i, 2) = a
            i = i + 1
    Next a
End Sub

Sub trythisA()
    Dim i As Long
    i = 1
    For Each a In Array("xlInsideHorizontal", "xlInsideVertical", "xlEdgeLeft", "xlEdgeRight", "xlEdgeBottom", "xlEdgeTop")
            Cells(i, 1) = a
            i = i + 1
    Next a
End Sub

我真的想避免保留两个独立的数组;一个用于文本字符串,另一个用于枚举。

有没有办法从文本字符串中获取枚举,或者将枚举转换为文本字符串?

任何建议都将受到赞赏。

1 个答案:

答案 0 :(得分:5)

我不相信你需要以编程方式生成这样的列表才能创建它。 Microsoft在MSDN中提供了所有这些的定义:

https://msdn.microsoft.com/en-us/vba/excel-vba/articles/constants-enumeration-excel

您可以从那里下载并将其粘贴到电子表格中。您甚至可以通过Web查询使其动态化,以便您可以随时了解任何更改。这是一个可以做到这一点的宏:

Sub GetEnumerationDefinitions()

    ActiveWorkbook.Queries.Add Name:="Enumerations", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://msdn.microsoft.com/en-us/vba/excel-vba/articles/constants-enumeration-excel""))," & Chr(13) & "" & Chr(10) & "    Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Name"", type text}, {""Value"", Int64.Type}, {""Description"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
    ActiveWorkbook.Worksheets.Add
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Enumerations"";Extended Properties=""""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [Enumerations]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_0"
        .Refresh BackgroundQuery:=False
    End With
End Sub