【发布时间】:2019-09-18 13:59:44
【问题描述】:
VBA 知识渊博的人,
我有一个数据透视图,我写了一个 sub 来格式化系列表示。此图表包含四个系列并连接到切片器。
问题在于格式不适用于某些切片器的按钮,因为其中一种系列格式消失了。这个系列格式应该是一条灰线;数据点在那里,但没有线条和填充颜色。
我已经调试了这个东西并用手表检查了发生了什么,但一切都很好并且运行正常。当我 F8 宏时,在系列不起作用之后,我尝试使用鼠标强制图形线上的颜色并且它起作用。
您对我应该在哪里寻找问题有什么建议吗?您的数据透视图也会出现这种情况吗?
我写了这段代码:
Dim srs_name As String
Dim srs As Integer
ActiveSheet.ChartObjects("Diagramm 7").Activate
'formatting Shipped Qty series
srs_name = "Shipped qty"
On Error Resume Next
If Not ActiveChart.SeriesCollection(srs_name) Is Nothing Then
ActiveChart.SeriesCollection(srs_name).ChartType = xlColumnStacked
ActiveChart.SeriesCollection(srs_name).Format.Fill.ForeColor.RGB = RGB(255, 192, 0)
ActiveChart.SeriesCollection(srs_name).Format.Line.ForeColor.RGB = RGB(255, 192, 0)
ActiveChart.SeriesCollection(srs_name).AxisGroup = 1
End If
'formatting Order series
srs_name = "Order"
On Error Resume Next
If Not ActiveChart.SeriesCollection(srs_name) Is Nothing Then
ActiveChart.SeriesCollection(srs_name).ChartType = xlColumnStacked
ActiveChart.SeriesCollection(srs_name).Format.Fill.ForeColor.RGB = RGB(91, 155, 213)
ActiveChart.SeriesCollection(srs_name).Format.Line.ForeColor.RGB = RGB(91, 155, 213)
ActiveChart.SeriesCollection(srs_name).AxisGroup = 1
End If
'formatting Sales series
srs_name = "Sales"
On Error Resume Next
If Not ActiveChart.SeriesCollection(srs_name) Is Nothing Then
ActiveChart.SeriesCollection(srs_name).ChartType = xlLine
ActiveChart.SeriesCollection(srs_name).Format.Fill.ForeColor.RGB = RGB(165, 165, 165)
ActiveChart.SeriesCollection(srs_name).Format.Line.ForeColor.RGB = RGB(165, 165, 165)
ActiveChart.SeriesCollection(srs_name).AxisGroup = 1
End If
'formatting Transport series
srs_name = "Transport"
On Error Resume Next
If Not ActiveChart.SeriesCollection(srs_name) Is Nothing Then
ActiveChart.SeriesCollection(srs_name).ChartType = xlColumnStacked
ActiveChart.SeriesCollection(srs_name).Format.Fill.ForeColor.RGB = RGB(237, 125, 49)
ActiveChart.SeriesCollection(srs_name).Format.Line.ForeColor.RGB = RGB(237, 125, 49)
ActiveChart.SeriesCollection(srs_name).AxisGroup = 1
Else
End If
'formatting a 5th series, if needed
srs = ActiveSheet.ChartObjects("Diagramm 7").Chart.SeriesCollection.Count
If srs > 4 Then
ActiveChart.SeriesCollection(5).ChartType = xlArea
ActiveChart.SeriesCollection(5).Format.Fill.ForeColor.RGB = RGB(222, 235, 247)
ActiveChart.SeriesCollection(5).Format.Line.ForeColor.RGB = RGB(222, 235, 247)
ActiveChart.SeriesCollection(5).AxisGroup = 1
End If
【问题讨论】:
-
如果您注释掉
On Error Resume Next的这些实例,您会收到错误消息吗?如果是这样,消息是什么,是哪一行抛出的? -
你好,@BigBen,当我注释掉这些实例时,我没有收到任何错误。
-
@SSGrace 您使用
On Error Resume Next的方式是最糟糕的做法(它隐藏了所有错误消息,但错误仍然存在,如果您的代码中出现问题,您永远不会注意到)。切勿单独使用。有关更多信息,您可能会受益于阅读VBA Error Handling – A Complete Guide -
嗨,@Pᴇʜ,我不知道,但我会从现在开始看,谢谢你的信息!
-
除了on-error-resume-next,你还有什么建议吗@Pᴇʜ?
标签: excel vba format excel-charts pivot-chart