Search code examples
excelvbaformatexcel-chartspivot-chart

Pivot chart formats


very VBA-knowledgeable people,

I have a pivot chart going on, and I wrote a sub to format the series representation. This chart contains four series and is connected to a slicer.

The problem is that the formats aren't working for some of the slicer's buttons, because one of the series formats disappear. This series format should be a grey line; the data points are there, but the line and fill color are absent.

I already debuged the thing and used the watch to check what's going on, but everything's fine and running as it should. When I F8 the macro, after the series that isn't working, I try using the mouse to force the color on the graph line and it works.

Do you have any suggestion of where should I look for the problema? Does this also happen to your pivot charts?

I wrote this code:

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




Solution

  • SOLVED: I just needed to add a ActiveChart.SeriesCollection(srs_name).Format.Line.Visible = True line.

    This is how it got:

    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