有没有办法测试Excel中的图表是否堆叠了其系列

时间:2014-12-01 14:40:28

标签: vba excel-vba excel

据我所知,Chars拥有ChartType as XlChartType属性,但这是一个很长的枚举值列表。有没有办法测试图表是否使用堆叠系列而不是全部列出?

我试图避免以下情况:

Select ActiveChart.ChartType
    Case xlAreaStacked 
        ....
    Case xlBarStacked
        ....
    Case xlColumnStacked 
        ....
   ... 1000 more Cases ....
End Select

1 个答案:

答案 0 :(得分:1)

下面的一些示例代码用于生成包含所请求枚举的成员的字典对象。

代码改编自dlmille的回答:http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_27613392.html

Sub tester()

    Dim dict

    Set dict = GetEnumLookup("Excel", "XlChartType")
    If Not dict Is Nothing Then
        'get string from numeric value and see if it contains "stacked"
        Debug.Print UCase( dict(XlChartType.xl3DAreaStacked) ) Like "*STACKED*"
        Debug.Print UCase( dict(XlChartType.xl3DArea) ) Like "*STACKED*"
    Else
        MsgBox "Enum not recognised!"
    End If


End Sub

'VB Project References required:
' Microsoft Visual Basic for Applications Extensibility
' TypeLib Information
Function GetEnumLookup(LibName As String, sEnumName As String) As Object

    Dim rv As Object
    Dim tl As TLI.TypeLibInfo
    Dim mi As TLI.MemberInfo
    Dim tiEnum As TLI.TypeInfo

    Dim vbProj As VBProject, oVBProjRef As Reference
    Set vbProj = ThisWorkbook.VBProject
    For Each oVBProjRef In vbProj.References

        'Debug.Print oVBProjRef.Name, oVBProjRef.FullPath
        If oVBProjRef.Name = LibName Then

            Set tl = New TypeLibInfo
            tl.ContainingFile = oVBProjRef.FullPath

            On Error Resume Next
            Set tiEnum = tl.GetTypeInfo(sEnumName)
            On Error GoTo 0

            If Not tiEnum Is Nothing Then
                Set rv = CreateObject("scripting.dictionary")
                For Each mi In tiEnum.Members
                     rv.Add mi.Value, mi.Name
                     'or if you want to map the other direction...
                     'rv.Add mi.Name, mi.Value
                Next mi
            End If

            Exit For
        End If

    Next oVBProjRef

    Set GetEnumLookup = rv
End Function