Create and link pivot chart to pivot table !!!!

adr12345

Board Regular
Joined
Jan 19, 2015
Messages
67
I have written a code for creating pivot table. After this I want to add Chart which is linked to PT1 pivot table. and I should be able to give exact cell location where that chart should appear.

Can anyone help how to do it? Its kinda urgent!!!



Set data1 = ActiveWorkbook.PivotCaches.Create(xlDatabase, range("A1", Cells(B, A)).Address(, , xlR1C1))
Set PT1 = data1.CreatePivotTable(Worksheets("Tables").range("B3"))

Set PT1 = data1.CreatePivotTable(Worksheets("Tables").range("B83"))

With PT1
With .PivotFields("name")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Job type")
.Orientation = xlRowField
.Position = 2
End With
With .PivotFields("Multiple Bids")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("ASR Id")
.Orientation = xlDataField
.Function = xlCount
.Position = 1
.Caption = "Volume"
End With

With .PivotFields("Annual Cost")
.Orientation = xlDataField
.Function = xlSum
.Position = 2
.Caption = "Annual Sum"
End With

With PT1
.MergeLabels = True
.InGridDropZones = True
.RowAxisLayout xlTabularRow
.SmallGrid = True
.ShowTableStyleColumnHeaders = True
.LayoutRowDefault = xlTabularRow
End With
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
The following code adds a pivot chart on the same worksheet as the pivot table. Try adding the following code at the end of the existing one...

Code:
[COLOR=darkblue]Dim[/COLOR] oChart [COLOR=darkblue]As[/COLOR] Chart

[COLOR=darkblue]Set[/COLOR] oChart = PT1.Parent.Shapes.AddChart(XlChartType:=xlLine).Chart

oChart.SetSourceData Source:=PT1.TableRange1

Change the chart type, accordingly.

Hope this helps!
 
Upvote 0
Hi Domenic,

Thanks it is working.

However is there any method by which I can specify the cell location for that graph?
Coz I have multiple tables and if I repeat this code for all tables then all the Graphs are overlapping on each other.

Or can the code itself identify all the pivot tables in sheet and generate graphs for all the pivots ? using some kind of loop..


Thanks !!
 
Upvote 0
Try...

Code:
Set oChart = PT1.Parent.Shapes.AddChart(XlChartType:=xlLine, Left:=Range("D2"), Top:=Range("D2"), Width:=350, Height:=200).Chart

Note, the width and height are optional.

Hope this helps!
 
Upvote 0
Left and top will decide the location of that chart right?

Thanks a tons..
M half way through my automation now :) yayyyy
 
Upvote 0

Forum statistics

Threads
1,216,110
Messages
6,128,896
Members
449,477
Latest member
panjongshing

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