Creating Graphs From Pivot Tables Using Macro

3LydonsOnAShirt

New Member
Joined
May 28, 2012
Messages
10
Hi there,

I'm fairly new to recording macro's. I've attempted to create one to consolidate some data, then create a pivot table from this, and lastly, create a graph from the pivot table.

The macro appears to work fine all the way through, until it comes to the Source Data for the Graph.

I've researched the errors & tried to play around with it but can't get it to work. The main error I'm getting is:

"Run time error '1004': Method 'Range' of object '_Global' failed"

My code (from Pivot creation to end) looks like this... I've coloured the errored line red:


Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Consolidated Data!R1C1:R442C8", Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="", TableName:="PivotTable1" _
, DefaultVersion:=xlPivotTableVersion14
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Category")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Month")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Time"), "Sum of Time", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Time")
.Calculation = xlPercentOfRow
.NumberFormat = "0.00%"
End With
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnStacked
ActiveChart.SetSourceData Source:=Range("PivotTable1")
ActiveChart.Location Where:=xlLocationAsObject, Name:="Graph"
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Pivot"
Sheets("Pivot").Select
Sheets("Pivot").Move After:=Sheets(3)
ActiveWorkbook.Save
End Sub


The strange thing is, despite the errors, it still creates both the pivot & the graph, and they're correct, so I'm not sure the reason for the error?

Any suggestions would be greatly appreciated.
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,214,583
Messages
6,120,377
Members
448,955
Latest member
BatCoder

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