Create pivot chart from pivot table (XL2007)

AOB

Well-known Member
Joined
Dec 15, 2010
Messages
657
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
Hi guys,

Trying to automate the generation of a pivot chart using VBA

Have managed to create the pivot table fine - all fields set up (pages, rows, columns, data) and it looks fine. I now want to create a pivot chart based on this pivot table.

I'm running into trouble when I try to set the source data for the chart :

Code:
Dim objWorksheetPivotTable As Excel.Worksheet
Dim objPivotCache As Excel.PivotCache
Dim objPivotTable As Excel.PivotTable
Dim objPivotChart As Variant
....
With objWorksheetPivotTable

    Set objPivotChart = .Shapes.AddChart

    With objPivotChart
        [COLOR=#ff0000].SetSourceData objPivotTable.TableRange1[/COLOR]
        .ChartType = xlColumnClustered
        .Location xlLocationAsNewSheet, "Pivot Chart"
    End With

End With

I get the following error :

Error 438 : Object doesn't support this property or method

I've tried defining objPivotChart a number of ways (as a Chart, as a Shape - I'm just using Variant now so I can let Excel assign it to suit)

How do I create my pivot chart, based on the existing pivot table?

Thanks

Al
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Shapes.Addchart actually returns a Shape, not a Chart, so you need:
Code:
 Set objPivotChart = .Shapes.AddChart.Chart
 
Upvote 0
Sorry Rory, another question

Trying to format each series by name - setting to a clustered column type, then specify the colour, linestyle etc.

How does one alter the lines (borders?) of the columns and specify the style, weight, color etc.? Similarly for the interior / fill? Can't get anything from intellisense!
 
Upvote 0
For example:
Code:
    With ActiveChart.SeriesCollection(3)
        With .Format.Fill
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorAccent3
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = 0.400000006
            .Transparency = 0
            .Solid
        End With
        With .Format.Line
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorAccent3
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = 0
            .Transparency = 0
            .Weight = 2
        End With
    End With
 
Upvote 0
Can the line / fill colours not be set using RGB notation?

It's not liking this :

Code:
With .Format
  With .Line
    .Visible = True
    [COLOR=#ff0000].ForeColor.RGB = RGB(69, 98, 135)[/COLOR]
  End With
  With .Fill
    [COLOR=#ff0000].ForeColor.RGB = RGB(92, 131, 180)[/COLOR]
    .Solid
  End With
End With

Error -2147417851 : Automation error
The server threw an exception
 
Upvote 0
Works fine for me in 2010. What are you using?
 
Upvote 0
2007

Actually, it seems to be a deeper problem - if I skip past this piece and on to playing with the data labels, I get the same error. Even though, when I set a watch to the Series object, I can see those properties present (so the syntax appears correct)

FYI - I am not running this directly from Excel, this code resides in Access and I am running it through an Excel.Application object. Just to clarify - far as I am aware, this shouldn't be a problem?...

Code:
With objWorksheetPivotTable
  Set objPivotChart = .Shapes.AddChart.Chart
  With objPivotChart
    .SetSourceData objPivotTable.TableRange1
    .ChartType = xlColumnClustered
    .Location xlLocationAsNewSheet, "Pivot Chart"
    For Each objSeries In .SeriesCollection
      With objSeries
        Select Case .Name
          Case "Volumes"
            .AxisGroup = 1
            .ChartType = xlColumnClustered
            With .Format
              With .Line
                .Visible = True
[COLOR=#ff0000]                .ForeColor.RGB = RGB(144, 60, 57)[/COLOR]
              End With
              With .Fill
[COLOR=#ff0000]                .ForeColor.RGB = RGB(192, 80, 77)[/COLOR]
                .Solid
              End With
            End With
[COLOR=#ff0000]            .HasDataLabels = True[/COLOR]
            With .DataLabels
              .Alignment = 90
              .Position = xlLabelPositionOutsideEnd
              With .Font
                .Bold = True
                .Size = 8
                .Color = RGB(144, 60, 57)
              End With
            End With
            ....
 
Upvote 0
It shouldn't be a problem, but Excel 2007 is buggy as hell especially where its new charting engine is concerned. Is the application/workbook visible while the code is running?
 
Upvote 0

Forum statistics

Threads
1,214,405
Messages
6,119,320
Members
448,887
Latest member
AirOliver

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top