使用VBA的Excel图表的主题图表样式
我正在尝试为仪表板中的Excel图表构build一些自定义主题。 在录制一个macros来看这些是如何实现的时候,这个macroslogging了下面的代码;
ActiveChart.ClearToMatchStyle ActiveChart.ChartStyle = 268
我通过谷歌search高低search这些chartstyles,或任何文档如何定制它们的列表。 每个search都会返回图表types常量的链接,即xlLine,xlPie等。而不是Excelfunction区中“图表工具 – devise”选项卡上的主题图表。
如果任何人都可以指出我正确的方向,将不胜感激。
编辑:
这些图表样式常量的最小或不可用文档,所以我创build了一个示例工作簿与所有图表样式types显示为饼图。 它在这里可用。 至less在selecttypes之前,您将拥有图表的可视化表示。
该工作簿可以在这里查看,如果有人知道如何添加一个可下载的版本的post请评论
在这里查看工作簿
您可以使用以下代码自行构build它,只需添加一个名为ChartStyles的表并创build一个名为GolfRoundsPlayed的数据表并使用此数据
每月回合已赛1月42 2月53 3月77 4月124 5月198 6月288 7月312 8月303 9月264 10月149 11月54 12月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
进一步对MSDN发表评论:
您可以使用从1到48的数字来设置图表样式。
我的testing显示范围201至352也是有效的。 这是所有图表types。
创build一个Excel工作簿看起来像这样 – 注意我已经添加了一个图表,所以ws.ChartObjects(1)
可以引用一些东西:
运行这个代码 – 你可以用Stop
来玩,看看更详细的情况。
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
GetChartTypes
的代码基于这个表格:
| GROUPING | 图表types| VALUE | VBA CONSTANT | | ------------- | ----------------------------------- ------------ | ------- | ---------------------------- | | 3DAREA | 3D AREA | -4098 | xl3DArea | | 3DAREA | 3D堆积区| 78 | xl3DAreaStacked | | 3DAREA | 3D 100%堆积区| 79 | xl3DAreaStacked100 | | 3DBAR | 3D CLUSTERED BAR | 60 | xl3DBarClustered | | 3DBAR | 3D STACKED BAR | 61 | xl3DBarStacked | | 3DBAR | 3D 100%堆叠酒吧| 62 | xl3DBarStacked100 | | 3DCOLUMN | 3D集群| 54 | xl3DColumnClustered | | 3DCOLUMN | 3D COLUMN | -4100 | xl3DColumn | | 3DCOLUMN | 3D CONE COLUMN | 105 | xlConeCol | | 3DCOLUMN | 3D圆柱| 98 | xlCylinderCol | | 3DCOLUMN | 3D金字塔| 112 | xlPyramidCol | | 3DCOLUMN | 3D STACKED COLUMN | 55 | xl3DColumnStacked | | 3DCOLUMN | 3D 100%堆积柱| 56 | xl3DColumnStacked100 | | AREA | AREA | 1 | xlArea | | AREA | 堆积区| 76 | xlAreaStacked | | AREA | 100%堆积区| 77 | xlAreaStacked100 | | BAR | 集群酒吧| 57 | xlBarClustered | | BAR | 堆叠的酒吧| 58 | xlBarStacked | | BAR | 100%堆叠酒吧| 59 | xlBarStacked100 | | 泡泡| 3D泡泡,3D效果泡泡 87 | xlBubble3DEffect | | 泡泡| 泡泡| 15 | xlBubble | | COLUMN | 集群| 51 | xlColumnClustered | | COLUMN | 堆积列| 52 | xlColumnStacked | | COLUMN | 100%堆积柱| 53 | xlColumnStacked100 | | CONE | 集群锥体| 99 | xlConeColClustered | | CONE | 堆积锥体| 100 | xlConeColStacked | | CONE | 100%堆焊锥体| 101 | xlConeColStacked100 | | CONEBAR | 群集的圆锥| 102 | xlConeBarClustered | | CONEBAR | 堆叠锥形杆| 103 | xlConeBarStacked | | CONEBAR | 100%堆焊锥形棒| 104 | xlConeBarStacked100 | | CYLINDER | 集群圆柱| 92 | xlCylinderColClustered | | CYLINDER | 堆焊圆柱| 93 | xlCylinderColStacked | | CYLINDER | 100%堆焊圆柱| 94 | xlCylinderColStacked100 | | CYLINDERBAR | 集群圆柱| 95 | xlCylinderBarClustered | | CYLINDERBAR | 堆焊圆柱| | 96 | xlCylinderBarStacked | | CYLINDERBAR | 100%堆焊圆柱| | 97 | xlCylinderBarStacked100 | | DONUT | DONUT | -4120 | xlDoughnut | | DONUT | 分解的DONUT | 80 | xlDoughnutExploded | | LINE | 3D LINE | -4101 | xl3DLine | | LINE | LINE | 4 | xlLine | | LINE | LINE与标记| 65 | xlLineMarkers | | LINE | 堆积线| 63 | xlLineStacked | | LINE | 100%堆叠线| 64 | xlLineStacked100 | | LINE | 堆叠线与标记| 66 | xlLineMarkersStacked | | LINE | 100%堆叠线与标记| 67 | xlLineMarkersStacked100 | | PIE | 3D PIE | -4102 | xl3DPie | | PIE | 3D分解PIE | 70 | xl3DPieExploded | | PIE | PIE | 71 | xlBarOfPie | | PIE | 爆炸的PIE | 69 | xlPieExploded | | PIE | PIE | 5 | xlPie | | PIE | PIE PIE | 68 | xlPieOfPie | | PYRAMID | 集群金字塔| 109 | xlPyramidBarClustered | | PYRAMID | 堆叠的金字塔| 110 | xlPyramidBarStacked | | PYRAMID | 100%堆叠金字塔| 111 | xlPyramidBarStacked100 | | PYRAMID | 集群金字塔| 106 | xlPyramidColClustered | | PYRAMID | 堆叠金字塔| 107 | xlPyramidColStacked | | PYRAMID | 100%堆叠金字塔| 108 | xlPyramidColStacked100 | | 雷达| 雷达| -4151 | xlRadar | | 雷达| 充满雷达| 82 | xlRadarFilled | | 雷达| 雷达与数据标记| 81 | xlRadarMarkers | | SCATTER | SCATTER | -4169 | xlXYScatter | | SCATTER | 散布与行| 74 | xlXYScatterLines | | SCATTER | 散布有线和没有数据标记| 75 | xlXYScatterLinesNoMarkers | | SCATTER | 散热片散热片| 72 | xlXYScatterSmooth | | SCATTER | 散热片散热片,没有数据标记| 73 | xlXYScatterSmoothNoMarkers | | 股票| 股票HLC(高低closures)| 88 | xlStockHLC | | 股票| 股票OHLC(开高 - 低 - 关)| 89 | xlStockOHLC | | 股票| 股票VHLC(音量高低closures)| 90 | xlStockVHLC | | 股票| 股票VOHLC(音量开 - 高 - 低 - 关)| 91 | xlStockVOHLC | | SURFACE | 3D SURFACE | 83 | xlSurface | | SURFACE | 3D表面丝网| 84 | xlSurfaceWireframe | | SURFACE | 表面顶视图| 85 | xlSurfaceTopView | | SURFACE | 表面俯视图WIREFRAME | 86 | xlSurfaceTopViewWireframe |
HTH