使用 VBA 的 Excel 图表的主题图表样式 [英] Themed Chart Styles for Excel charts using VBA

查看:92
本文介绍了使用 VBA 的 Excel 图表的主题图表样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为仪表板中的 excel 图表构建一些自定义主题.在录制宏以查看这些是如何实现的时,宏录制了以下代码;

ActiveChart.ClearToMatchStyleActiveChart.ChartStyle = 268

我通过谷歌搜索了高低,找到了这些图表样式的列表,或任何关于如何自定义它们的文档.每次搜索都会返回图表类型常量的链接,即 xlLine、xlPie 等.而不是 Excel 功能区的图表工具 - 设计"选项卡上可用的主题图表.

如果有人能指出我正确的方向,我将不胜感激.

这些图表样式常量几乎没有可用的文档,因此我创建了一个示例工作簿,其中所有图表样式类型都显示为饼图.它可供您使用.至少在选择类型之前,您将有一个图表的可视化表示.

可以在这里查看工作簿,如果有人知道如何在帖子中添加可下载版本,请评论

运行此代码 - 您可以使用 Stop 来查看更详细的情况.

选项显式子理解图表样式()Dim ws As 工作表Dim cht as ChartObjectDim varTypes As VariantDim i 为整数,j 为整数varTypes = GetChartTypes设置 ws = ThisWorkbook.Worksheets(1)设置 cht = ws.ChartObjects(1)对于 j = LBound(varTypes) 到 UBound(varTypes)cht.Chart.ChartType = varTypes(j)对于 i = 1 到 1000出错时继续下一步cht.Chart.ChartStyle = i如果 Err.Number = 0 那么Debug.Print "图表类型:" &varTypes(j) &"; 图表样式:" &我&"; 总和:" &varTypes(j) + i别的Debug.Print "图表样式错误:" &一世万一停止接下来我停止下一个结束子函数 GetChartTypes() 作为变体Dim i 作为整数Dim varTypes(1 to 73) 作为整数varTypes(1) = -4169varTypes(2) = -4151varTypes(3) = -4120varTypes(4) = -4102varTypes(5) = -4101varTypes(6) = -4100varTypes(7) = -4098变量类型(8) = 1变量类型(9) = 4变量类型(10)= 5变量类型(11)= 15对于 i = 12 到 73varTypes(i) = i + 39接下来我GetChartTypes = varTypes结束函数

GetChartTypes 的代码基于此表:

<前>|分组|图表类型 |价值 |VBA 常数 ||------------- |------------------------------------------------ |------- |---------------------------- ||3DAREA |3D 区域 |-4098 |xl3DA区||3DAREA |3D 堆叠区域 |78 |xl3DAreaStacked ||3DAREA |3D 100% 堆叠区域 |79 |xl3DAreaStacked100 ||3DBAR |3D 集群酒吧 |60 |xl3DBarClustered ||3DBAR |3D 堆叠条 |61 |xl3DBarStacked ||3DBAR |3D 100% 堆叠条形 |62 |xl3DBarStacked100 ||3DCOLUMN |3D 集群列 |54 |xl3DColumnClustered ||3DCOLUMN |3D 列 |-4100 |xl3DColumn ||3DCOLUMN |3D 锥柱 |105 |xlConeCol ||3DCOLUMN |3D 圆柱体 |98 |xlCylinderCol ||3DCOLUMN |3D 金字塔柱 |112 |xlPyramidCol ||3DCOLUMN |3D 堆叠柱 |55 |xl3DColumnStacked ||3DCOLUMN |3D 100% 堆叠列 |56 |xl3DColumnStacked100 ||面积 |面积 |1 |xl面积||面积 |堆积区 |76 |xlAreaStacked ||面积 |100% 堆叠区域 |77 |xlAreaStacked100 ||酒吧 |簇状酒吧 |57 |xlBarClustered ||酒吧 |堆叠的酒吧 |58 |xlBarStacked ||酒吧 |100% 堆叠酒吧 |59 |xlBarStacked100 ||泡泡 |3D 泡泡,带 3D 效果的泡泡 |87 |xlBubble3DEffect ||泡泡 |泡泡 |15 |xlBubble ||专栏 |群集列 |51 |xlColumnClustered ||专栏 |堆叠柱 |52 |xlColumnStacked ||专栏 |100% 堆叠列 |53 |xlColumnStacked100 ||锥体 |成簇锥柱 |99 |xlConeColClustered ||锥体 |堆叠锥柱 |100 |xlConeColStacked ||锥体 |100% 堆叠锥柱 |101 |xlConeColStacked100 ||锥体 |成簇锥棒|102 |xlConeBarClustered ||锥体 |堆叠锥棒 |103 |xlConeBarStacked ||锥体 |100% 堆叠锥棒 |104 |xlConeBarStacked100 ||气缸 |成簇的圆柱体 |92 |xlCylinderColClustered ||气缸 |堆叠式圆柱体 |93 |xlCylinderColStacked ||气缸 |100% 堆叠圆柱体 |94 |xlCylinderColStacked100 ||气缸杆 |簇状圆柱棒|95 |xlCylinderBarClustered ||气缸杆 |堆叠式气缸棒 |96 |xlCylinderBarStacked ||气缸杆 |100% 堆叠式气缸棒 |97 |xlCylinderBarStacked100 ||甜甜圈 |甜甜圈 |-4120 |xl甜甜圈||甜甜圈 |炸甜甜圈 |80 |xlDoughnutExploded ||线 |3D 线 |-4101 |xl3DLine ||线 |线 |4 |xlLine ||线 |带标记的线 |65 |xlLineMarkers ||线 |堆叠线 |63 |xlLineStacked ||线 |100% 堆叠线 |64 |xlLineStacked100 ||线 |带标记的堆叠线 |66 |xlLineMarkersStacked ||线 |100% 带标记的堆叠线 |67 |xlLineMarkersStacked100 ||派 |3D 饼图 |-4102 |xl3DPie ||派 |3D 爆炸饼 |70 |xl3DPieExploded ||派 |馅饼酒吧 |71 |xlBarOfPie ||派 |炸馅饼 |69 |xlPieExploded ||派 |派 |5 |xlPie ||派 |馅饼68 |xlPieOfPie ||金字塔 |簇状金字塔酒吧 |109 |xlPyramidBarClustered ||金字塔 |堆叠金字塔酒吧 |110 |xlPyramidBarStacked ||金字塔 |100% 堆叠金字塔酒吧 |111 |xlPyramidBarStacked100 ||金字塔 |聚簇金字塔柱 |106 |xlPyramidColClustered ||金字塔 |堆叠金字塔柱 |107 |xlPyramidColStacked ||金字塔 |100% 堆叠金字塔柱 |108 |xlPyramidColStacked100 ||雷达 |雷达 |-4151 |xl雷达||雷达 |填充雷达 |82 |xlRadarFilled ||雷达 |带有数据标记的雷达 |81 |xlRadarMarkers ||分散 |分散 |-4169 |xlXYScatter ||分散 |用线条分散 |74 |xlXYScatterLines ||分散 |带有线条且无数据标记的散点图 |75 |xlXYScatterLinesNoMarkers ||分散 |用平滑的线条分散 |72 |xlXYScatterSmooth ||分散 |具有平滑线条且无数据标记的散射 |73 |xlXYScatterSmoothNoMarkers ||股票 |股票 HLC(高-低-收盘)|88 |xlStockHLC ||股票 |股票 OHLC(开-高-低-闭)|89 |xlStockOHLC ||股票 |股票 VHLC(成交量-高-低-收盘)|90 |xlStockVHLC ||股票 |STOCK VOHLC (VOLUME-OPEN-HIGH-LOW-CLOSE) |91 |xlStockVOHLC ||表面 |3D 表面 |83 |xl表面||表面 |3D 表面线框 |84 |xlSurfaceWireframe ||表面 |表面顶视图 |85 |xlSurfaceTopView ||表面 |表面顶视图线框 |86 |xlSurfaceTopViewWireframe |

HTH

I am trying to build some custom themes for excel charts in a dashboard. Upon recording a macro to see how these are implemented, the macro recorded the following code;

ActiveChart.ClearToMatchStyle
ActiveChart.ChartStyle = 268

I have searched high and low via google to find a list of these chartstyles, or any documentation on how to customize them. Every search returns links for the chart type constants, i.e. xlLine, xlPie etc. NOT the themed charts available on the Chart Tools-Design tab in the Excel ribbon.

If anyone can point me in the right direction it would be much appreciated.

EDIT:

There is minimal to no documentation available for these Chart Style constants, so I created a sample workbook with all of the Chart Style types displayed as pie charts. It is available for you here. at least you will have a visual representation of the chart prior to choosing the type.

The workbook can be viewed here, if anyone knows how to add a downloadable version in the post please comment

View the workbook here

You can build it yourself with the following code, just add a sheet named ChartStyles and create a data table named GolfRoundsPlayed and use this data

Month Rounds Played Jan 42 Feb 53 Mar 77 Apr 124 May 198 Jun 288 Jul 312 Aug 303 Sep 264 Oct 149 Nov 54 Dec 33

Sub BuildChartStyleSheet()
    Dim targetChart As Chart
    Dim targetSheet As Worksheet
    Dim top As Long
    Dim x As Integer, chtTitle As String
    top = 15

    Dim dataRange As Range
    Set dataRange = Range("GolfRoundsPlayed")
    Set targetSheet = Sheets("ChartStyles")

    Application.ScreenUpdating = False
    For x = 1 To 353
        If x > 1 Then top = top + 128
        On Error Resume Next
        Set targetChart = targetSheet.Shapes.AddChart2(x, xlPie, 2, top, 230, 125).Chart

        chtTitle = "ChartStyle for ChartStyle #" & x
        With targetChart
            .SetSourceData Source:=dataRange
            .chartTitle.Text = chtTitle
            .chartTitle.Format.TextFrame2.TextRange.Font.Size = 11
        End With
    Next x
   Application.ScreenUpdating = True
End Sub

解决方案

Further to comment on MSDN that:

You can use a number from 1 to 48 to set the chart style.

My test shows that range 201 to 352 is valid as well. This is across all chart types.

Create an Excel workbook looks like this - note I already added a chart so ws.ChartObjects(1) can reference something:

The run this code - you can play with the Stops to see what is happening in more detail.

Option Explicit

Sub UnderstandChartStyle()

    Dim ws As Worksheet
    Dim cht As ChartObject
    Dim varTypes As Variant
    Dim i As Integer, j As Integer

    varTypes = GetChartTypes
    Set ws = ThisWorkbook.Worksheets(1)
    Set cht = ws.ChartObjects(1)

    For j = LBound(varTypes) To UBound(varTypes)
        cht.Chart.ChartType = varTypes(j)
        For i = 1 To 1000
            On Error Resume Next
            cht.Chart.ChartStyle = i
            If Err.Number = 0 Then
                Debug.Print "Chart type: " & varTypes(j) & "; Chart style: " & i & "; Sum: " & varTypes(j) + i
            Else
                Debug.Print "Chart style error: " & i
            End If
            Stop
        Next i
        Stop
    Next j

End Sub

Function GetChartTypes() As Variant

    Dim i As Integer
    Dim varTypes(1 To 73) As Integer

    varTypes(1) = -4169
    varTypes(2) = -4151
    varTypes(3) = -4120
    varTypes(4) = -4102
    varTypes(5) = -4101
    varTypes(6) = -4100
    varTypes(7) = -4098
    varTypes(8) = 1
    varTypes(9) = 4
    varTypes(10) = 5
    varTypes(11) = 15

    For i = 12 To 73
        varTypes(i) = i + 39
    Next i

    GetChartTypes = varTypes

End Function

The code for GetChartTypes is based from this table:

| GROUPING      | CHART TYPE                                        | VALUE     | VBA CONSTANT                  |
|-------------  |-----------------------------------------------    |-------    |----------------------------   |
| 3DAREA        | 3D AREA                                           | -4098     | xl3DArea                      |
| 3DAREA        | 3D STACKED AREA                                   | 78        | xl3DAreaStacked               |
| 3DAREA        | 3D 100% STACKED AREA                              | 79        | xl3DAreaStacked100            |
| 3DBAR         | 3D CLUSTERED BAR                                  | 60        | xl3DBarClustered              |
| 3DBAR         | 3D STACKED BAR                                    | 61        | xl3DBarStacked                |
| 3DBAR         | 3D 100% STACKED BAR                               | 62        | xl3DBarStacked100             |
| 3DCOLUMN      | 3D CLUSTERED COLUMN                               | 54        | xl3DColumnClustered           |
| 3DCOLUMN      | 3D COLUMN                                         | -4100     | xl3DColumn                    |
| 3DCOLUMN      | 3D CONE COLUMN                                    | 105       | xlConeCol                     |
| 3DCOLUMN      | 3D CYLINDER COLUMN                                | 98        | xlCylinderCol                 |
| 3DCOLUMN      | 3D PYRAMID COLUMN                                 | 112       | xlPyramidCol                  |
| 3DCOLUMN      | 3D STACKED COLUMN                                 | 55        | xl3DColumnStacked             |
| 3DCOLUMN      | 3D 100% STACKED COLUMN                            | 56        | xl3DColumnStacked100          |
| AREA          | AREA                                              | 1         | xlArea                        |
| AREA          | STACKED AREA                                      | 76        | xlAreaStacked                 |
| AREA          | 100% STACKED AREA                                 | 77        | xlAreaStacked100              |
| BAR           | CLUSTERED BAR                                     | 57        | xlBarClustered                |
| BAR           | STACKED BAR                                       | 58        | xlBarStacked                  |
| BAR           | 100% STACKED BAR                                  | 59        | xlBarStacked100               |
| BUBBLE        | 3D BUBBLE, BUBBLE WITH 3D EFFECTS                 | 87        | xlBubble3DEffect              |
| BUBBLE        | BUBBLE                                            | 15        | xlBubble                      |
| COLUMN        | CLUSTERED COLUMN                                  | 51        | xlColumnClustered             |
| COLUMN        | STACKED COLUMN                                    | 52        | xlColumnStacked               |
| COLUMN        | 100% STACKED COLUMN                               | 53        | xlColumnStacked100            |
| CONE          | CLUSTERED CONE COLUMN                             | 99        | xlConeColClustered            |
| CONE          | STACKED CONE COLUMN                               | 100       | xlConeColStacked              |
| CONE          | 100% STACKED CONE COLUMN                          | 101       | xlConeColStacked100           |
| CONEBAR       | CLUSTERED CONE BAR                                | 102       | xlConeBarClustered            |
| CONEBAR       | STACKED CONE BAR                                  | 103       | xlConeBarStacked              |
| CONEBAR       | 100% STACKED CONE BAR                             | 104       | xlConeBarStacked100           |
| CYLINDER      | CLUSTERED CYLINDER COLUMN                         | 92        | xlCylinderColClustered        |
| CYLINDER      | STACKED CYLINDER COLUMN                           | 93        | xlCylinderColStacked          |
| CYLINDER      | 100% STACKED CYLINDER COLUMN                      | 94        | xlCylinderColStacked100       |
| CYLINDERBAR   | CLUSTERED CYLINDER BAR                            | 95        | xlCylinderBarClustered        |
| CYLINDERBAR   | STACKED CYLINDER BAR                              | 96        | xlCylinderBarStacked          |
| CYLINDERBAR   | 100% STACKED CYLINDER BAR                         | 97        | xlCylinderBarStacked100       |
| DOUGHNUT      | DOUGHNUT                                          | -4120     | xlDoughnut                    |
| DOUGHNUT      | EXPLODED DOUGHNUT                                 | 80        | xlDoughnutExploded            |
| LINE          | 3D LINE                                           | -4101     | xl3DLine                      |
| LINE          | LINE                                              | 4         | xlLine                        |
| LINE          | LINE WITH MARKERS                                 | 65        | xlLineMarkers                 |
| LINE          | STACKED LINE                                      | 63        | xlLineStacked                 |
| LINE          | 100% STACKED LINE                                 | 64        | xlLineStacked100              |
| LINE          | STACKED LINE WITH MARKERS                         | 66        | xlLineMarkersStacked          |
| LINE          | 100% STACKED LINE WITH MARKERS                    | 67        | xlLineMarkersStacked100       |
| PIE           | 3D PIE                                            | -4102     | xl3DPie                       |
| PIE           | 3D EXPLODED PIE                                   | 70        | xl3DPieExploded               |
| PIE           | BAR OF PIE                                        | 71        | xlBarOfPie                    |
| PIE           | EXPLODED PIE                                      | 69        | xlPieExploded                 |
| PIE           | PIE                                               | 5         | xlPie                         |
| PIE           | PIE OF PIE                                        | 68        | xlPieOfPie                    |
| PYRAMID       | CLUSTERED PYRAMID BAR                             | 109       | xlPyramidBarClustered         |
| PYRAMID       | STACKED PYRAMID BAR                               | 110       | xlPyramidBarStacked           |
| PYRAMID       | 100% STACKED PYRAMID BAR                          | 111       | xlPyramidBarStacked100        |
| PYRAMID       | CLUSTERED PYRAMID COLUMN                          | 106       | xlPyramidColClustered         |
| PYRAMID       | STACKED PYRAMID COLUMN                            | 107       | xlPyramidColStacked           |
| PYRAMID       | 100% STACKED PYRAMID COLUMN                       | 108       | xlPyramidColStacked100        |
| RADAR         | RADAR                                             | -4151     | xlRadar                       |
| RADAR         | FILLED RADAR                                      | 82        | xlRadarFilled                 |
| RADAR         | RADAR WITH DATA MARKERS                           | 81        | xlRadarMarkers                |
| SCATTER       | SCATTER                                           | -4169     | xlXYScatter                   |
| SCATTER       | SCATTER WITH LINES                                | 74        | xlXYScatterLines              |
| SCATTER       | SCATTER WITH LINES AND NO DATA MARKERS            | 75        | xlXYScatterLinesNoMarkers     |
| SCATTER       | SCATTER WITH SMOOTH LINES                         | 72        | xlXYScatterSmooth             |
| SCATTER       | SCATTER WITH SMOOTH LINES AND NO DATA MARKERS     | 73        | xlXYScatterSmoothNoMarkers    |
| STOCK         | STOCK HLC (HIGH-LOW-CLOSE)                        | 88        | xlStockHLC                    |
| STOCK         | STOCK OHLC (OPEN-HIGH-LOW-CLOSE)                  | 89        | xlStockOHLC                   |
| STOCK         | STOCK VHLC (VOLUME-HIGH-LOW-CLOSE)                | 90        | xlStockVHLC                   |
| STOCK         | STOCK VOHLC (VOLUME-OPEN-HIGH-LOW-CLOSE)          | 91        | xlStockVOHLC                  |
| SURFACE       | 3D SURFACE                                        | 83        | xlSurface                     |
| SURFACE       | 3D SURFACE WIREFRAME                              | 84        | xlSurfaceWireframe            |
| SURFACE       | SURFACE TOP VIEW                                  | 85        | xlSurfaceTopView              |
| SURFACE       | SURFACE TOP VIEW WIREFRAME                        | 86        | xlSurfaceTopViewWireframe     |

HTH

这篇关于使用 VBA 的 Excel 图表的主题图表样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆