VBA create multiple pivot charts

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I can create a pivot chart from a pivot table, but sometimes when it creates the 2nd or 3rd one I get an error

Highlights ch.SetSourceData pt.TableRange1
'-2147467259(80004005): method 'setsourcedata' of object'_chart failed

VBA Code:
Set pt = mysheet.pivottables("pivot1")

Set chobj = mysheet.ChartObjects.Add(300, 200, 550, 200)

Set ch = chobj.Chart
ch.SetSourceData pt.TableRange1
ch.ChartType = xl3DColum

Set pt = mysheet.pivottables("pivot2")

Set chobj = mysheet.ChartObjects.Add(600, 200, 550, 200)

Set ch = chobj.Chart
ch.SetSourceData pt.TableRange1
ch.ChartType = xlbarclustered
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Make sure that the active cell in your workheet does not reside within the range of any pivot table when you run your macro. Otherwise, when you add the chartobject to your worksheet, it automatically sets the range for the source data to that of the pivot table where the active cell resides. Then, when you try to set the range for the source data, you'll get that error.

Better yet, in your code, add a new worksheet and create your pivot charts in this new sheet so you won't have to worry about where the active cell resides.

Hope this helps!
 
Last edited:
Upvote 0
Solution
Thanks Domenic

Didn't think cell was active within pivot table

Looks like any cell that surrounds any part of any pivot table affects it as well

I.e. if pivot table range is e2:f20 if active cell is in e1:G1 or G2:G20 it I'll give the error
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,872
Members
449,097
Latest member
dbomb1414

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