Put 2 Pivot Charts on one Worksheet

MacroAlan

New Member
Joined
Aug 30, 2004
Messages
31
I have built my file in Access, ported to Excel and now opened Excel to add PivotTables. The pivot tables are working great.

I added charts but both are showing the same; as though they are working off the same pivot table. I need the 2nd to work off the 2nd named pivot table.

I call the code to build the second:
Code:
PivotThe2nd Lrow, NewWS, RST!Pivot2Pos, RST!PivotSheet, RST!Piv2Nm
' Last row in datasheet, current worksheet name, Position for the Pivot (not working yet), Named pivot sheet, pivot table name
Then the macro creates the pivot table and attempts to create the Chart.
Code:
Sub PivotThe2nd(LastRow As LongPtr, SourceSht As String, PivPos As Integer, PivSht As String, PivShtNm As String)
    Dim Qut     As String
    Dim PC      As PivotCache
    Dim shtSrc  As Worksheet
    Dim shtDest As Worksheet

    Set shtSrc = Sheets(SourceSht)  'ActiveSheet
    Set shtDest = Sheets(PivSht)
    
    Set PC = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:=shtSrc.Range("A1").CurrentRegion)
    PC.CreatePivotTable tabledestination:=Range("M3"), _
        TableName:=PivShtNm
    With shtDest.PivotTables(PivShtNm)
        .InGridDropZones = True
        .RowAxisLayout xlTabularRow
    End With
    With shtDest
        .PivotTables(PivShtNm).AddDataField .PivotTables(PivShtNm).PivotFields("Order No"), "Count Order No", xlCount
        .PivotTables(PivShtNm).PivotFields("Order Date Month").Orientation = xlColumnField
        .PivotTables(PivShtNm).PivotFields("Lead Time Violation").Orientation = xlRowField
        .PivotTables(PivShtNm).PivotFields("NATL").Orientation = xlPageField
        .PivotTables(PivShtNm).PivotFields("NATL").CurrentPage = "Natl"
        .PivotTables(PivShtNm).ColumnGrand = False
        .PivotTables(PivShtNm).RowGrand = False
    End With
    shtDest.Shapes.AddChart2(297, xlColumnStacked).Select
    ActiveChart.SetSourceData source:=PivotTables(PivShtNm) 'not working ESP?
    Set shtDest = Nothing
    Set PC = Nothing
End Sub
How do I will the chart to use the “PivShtNm” pivot for its data?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
This is more of an excel question, however, try qualifying the sheet name and adding in DataBodyRange:

Code:
ActiveChart.SetSourceData source:=shtDest.PivotTables(PivShtNm).DataBodyRange
 
Last edited:
Upvote 0
I realized 5 minutes after posting that it was in the wrong place but got an error message when I tried to remedy.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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