数据透视图格式

Pivot chart formats

非常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



已解决:我只需要添加一条 ActiveChart.SeriesCollection(srs_name).Format.Line.Visible = True 行。

它是这样得到的:

Dim srs_name As String   
Dim srs As Integer

ActiveSheet.ChartObjects("Diagramm 7").Activate

         'formatting Shipped Qty series
srs_name = "Shipped qty"
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"
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"
If Not ActiveChart.SeriesCollection(srs_name) Is Nothing Then
    ActiveChart.SeriesCollection(srs_name).ChartType = xlLine
    ActiveChart.SeriesCollection(srs_name).Format.Line.Visible = True
    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"
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


我认为如果系列名称不存在,If Not ActiveChart.SeriesCollection(srs_name) Is Nothing Then 将抛出异常。相反,我推荐一个函数来测试 SeriesExists.

我还建议不要使用 ActivateActiveChart(参见 How to avoid using Select in Excel VBA)。而是按名称指定图表。

请注意,如果有 5ᵗʰ,则无需计算序列数。您可以只使用 SeriesExists(MyChart, 5) 来测试它。

我推荐如下内容:

Option Explicit

Public Sub FormatCharts()
    Dim ws As Worksheet
    Set ws = ActiveSheet 'better something like ThisWorkbook.Worksheets("Tabelle 1")

    Dim MyChart As Chart
    Set MyChart = ws.ChartObjects("Diagramm 7").Chart

    Dim srs_name As String

    'formatting Shipped Qty series
    srs_name = "Shipped qty"
    If SeriesExists(MyChart, srs_name) Then
        With MyChart.SeriesCollection(srs_name)
            .ChartType = xlColumnStacked
            .Format.Fill.ForeColor.RGB = RGB(255, 192, 0)
            .Format.Line.ForeColor.RGB = RGB(255, 192, 0)
            .AxisGroup = 1
        End With
    End If

   'formatting Order series
    srs_name = "Order"
    If SeriesExists(MyChart, srs_name) Then
        With MyChart.SeriesCollection(srs_name)
            .ChartType = xlColumnStacked
            .Format.Fill.ForeColor.RGB = RGB(91, 155, 213)
            .Format.Line.ForeColor.RGB = RGB(91, 155, 213)
            .AxisGroup = 1
        End With
    End If

    'formatting Sales series
    srs_name = "Sales"
    If SeriesExists(MyChart, srs_name) Then
        With MyChart.SeriesCollection(srs_name)
            .ChartType = xlLine
            .Format.Line.Visible = True
            .Format.Fill.ForeColor.RGB = RGB(165, 165, 165)
            .Format.Line.ForeColor.RGB = RGB(165, 165, 165)
            .AxisGroup = 1
        End With
    End If

    'formatting Transport series
    srs_name = "Transport"
    If SeriesExists(MyChart, srs_name) Then
        With MyChart.SeriesCollection(srs_name)
            .ChartType = xlColumnStacked
            .Format.Fill.ForeColor.RGB = RGB(237, 125, 49)
            .Format.Line.ForeColor.RGB = RGB(237, 125, 49)
            .AxisGroup = 1
        End With
    End If

    'formatting a 5th series, if needed
     If SeriesExists(MyChart, 5) Then 
        With MyChart.SeriesCollection(5)
            .ChartType = xlArea
            .Format.Fill.ForeColor.RGB = RGB(222, 235, 247)
            .Format.Line.ForeColor.RGB = RGB(222, 235, 247)
            .AxisGroup = 1
        End With
    End If
End Sub

Private Function SeriesExists(InChart As Chart, SeriesName As Variant) As Boolean
    Dim TestSeries As Series
    On Error Resume Next
    Set TestSeries = InChart.SeriesCollection(SeriesName)
    On Error GoTo 0
    SeriesExists = Not TestSeries Is Nothing
End Function