0

I have a sheet with Pivot table and I am creating an Column stacked chart from the table.

I would like to change the colours of Legend entries in chart and would like to have an title for the chart every time I create the chart.

I tried the below code and I'm getting

Object variable or with block variable not set

I get the error in the line

cht.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(155, 213, 91)

Could anyone, tell me how I could create chart for my requirements. I have added an image of my current chart and required chart.

Sub chart11()
    Dim sh As Shape
    Dim cht As Chart

    If ActiveSheet.PivotTables.Count = 0 Then Exit Sub

    Set ptable = ActiveSheet.PivotTables(1)
    Set ptr = ptable.TableRange1
    Set sh = ActiveSheet.Shapes.AddChart
    sh.Select

    With ActiveChart
        .SetSourceData ptr
        .ChartType = xlColumnStacked
    End With

    cht.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(155, 213, 91)
    cht.Axes(xlCategory).MinimumScale = 5
    cht.Axes(xlCategory).MaximumScale = 40
    cht.HasTitle = True    
    cht.ChartTitle.Text = "Default Chart"
End Sub

This is the chart generated by my code now, if I comment the error line:

This is the chart generated by my code now, if I comment the error line.

I would like to have a chart of this type with change in colour legends and chart title:

I would like to have a chart of this type with change in colour legends and chart title

6
  • You never assign cht to anything?! Commented Jul 21, 2017 at 12:18
  • @FunThomas means ? Commented Jul 21, 2017 at 12:23
  • means something like Set cht = ActiveChart is missing. You only declared cht as a chart but the variable is empty if you don't set it to some existing or new chart. Commented Jul 21, 2017 at 12:27
  • @Peh I will try this Commented Jul 21, 2017 at 13:01
  • 2
    You never defined ptabel or ptr. Make sure you always put Option Explicit on the top of your programs to prevent these errors in the future Commented Jul 21, 2017 at 14:41

1 Answer 1

1

You can't use cht.Axes(xlCategory).MinimumScale = 5 and MaximumScale. Because Pivot chart do not assist Scatterchart. Only in scatter chart, you can set xlCategory Scale.

Sub chart11()
    Dim sh As Shape
    Dim cht As Chart

    If ActiveSheet.PivotTables.Count = 0 Then Exit Sub

    Set ptable = ActiveSheet.PivotTables(1)
    Set ptr = ptable.TableRange1
    Set sh = ActiveSheet.Shapes.AddChart
    sh.Select
    Set cht = ActiveChart
    With cht
        .SetSourceData ptr
        .ChartType = xlColumnStacked
    End With

    cht.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0) '<~~ Red
    cht.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(0, 255, 0) '<~~ green
    'cht.Axes(xlCategory).MinimumScale = 5
    'cht.Axes(xlCategory).MaximumScale = 40
    cht.HasTitle = True
    cht.ChartTitle.Text = "Default Chart"
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

could you please provide me the colour code for the above graph , I have showed for green and Red
@Jenny: I modified my answer.
RGB is Red,Green,Blue of Light.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.