How to create multiple pivot chart based on different pivot tables on the same excel sheet?

Viewed 43

I would like to add multiple charts to the same sheet using different pivot table on the same sheets as well. However, whenever I try to make the second chart on the same sheet, it would only display the data from the first pivot table made. I have tried many different methods to amend the codes, but was unable to do so. Why is this so and any advice on this problem?

This is what happens when I run my codes, the second chart is not updated to the second pivot table

enter image description here

Sub test2()

    'Declare Variables
    Dim PSheet As Worksheet
    Dim DSheet As Worksheet
    Dim PCache As PivotCache
    Dim PTable As PivotTable
    Dim PRange As Range
    Dim lastrow As Long
    Dim LastCol As Long
    
    'Insert a New Blank Worksheet
    On Error Resume Next
    Application.DisplayAlerts = False
    
    Application.Run "Delete_Sheet3"
    
    Sheets.Add After:=Worksheets("Sheet2")
    
    ActiveSheet.name = "Sheet3"
    Application.DisplayAlerts = True
    Set PSheet = Worksheets("Sheet3")
    Set DSheet = Worksheets("Sheet1")
    
    'Define Data Range
    lastrow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
    LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    Set PRange = DSheet.Cells(1, 1).Resize(lastrow, LastCol)
    
    '---------- Comments ----------
    
    'Define Pivot Cache
    Set PCache = ActiveWorkbook.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=PRange). _
    CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
    TableName:="CommentPivotTable")
    
    'Insert Blank Pivot Table
    Set PTable = PCache.CreatePivotTable _
    (TableDestination:=PSheet.Cells(1, 1), TableName:="CommentPivotTable")
    
    'Insert Row Fields
    With ActiveSheet.PivotTables("CommentPivotTable").PivotFields("Comments")
        .Orientation = xlRowField
        .Position = 1
    End With
    
    'Insert Data Field
    With ActiveSheet.PivotTables("CommentPivotTable").PivotFields("Comments")
        .Orientation = xlDataField
        .Function = xlCount
        .name = "Count of Comments"
    End With
    
    '---------- Hold Code ----------
    
    Set PCache = Nothing
    Set PTable = Nothing

    'Define Pivot Cache
    Set PCache = ActiveWorkbook.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=PRange). _
    CreatePivotTable(TableDestination:=PSheet.Cells(22, 1), _
    TableName:="HoldTbl")
    
    'Insert Blank Pivot Table
    Set PTable = PCache.CreatePivotTable _
    (TableDestination:=PSheet.Cells(22, 1), TableName:="HoldTbl")
    
    'Insert Row Fields
    With ActiveSheet.PivotTables("HoldTbl").PivotFields("Hold Code")
        .Orientation = xlRowField
        .Position = 1
    End With
    
    'Insert Data Field
    With ActiveSheet.PivotTables("HoldTbl").PivotFields("Hold Code")
        .Orientation = xlDataField
        .Function = xlCount
        .name = "Count of Hold Code"
    End With
    
    '---------- Stat ----------
    
    Set PCache = Nothing
    Set PTable = Nothing

    'Define Pivot Cache
    Set PCache = ActiveWorkbook.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=PRange). _
    CreatePivotTable(TableDestination:=PSheet.Cells(44, 1), _
    TableName:="StatTbl")
    
    'Insert Blank Pivot Table
    Set PTable = PCache.CreatePivotTable _
    (TableDestination:=PSheet.Cells(44, 1), TableName:="StatTbl")
    
    'Insert Row Fields
    With ActiveSheet.PivotTables("StatTbl").PivotFields("Stat")
        .Orientation = xlRowField
        .Position = 1
    End With
    
    'Insert Data Field
    With ActiveSheet.PivotTables("StatTbl").PivotFields("Stat")
        .Orientation = xlDataField
        .Function = xlCount
        .name = "Count of Stat"
    End With
    
    ActiveSheet.UsedRange.EntireColumn.AutoFit
    
    
    '---------- Charts ----------

    Dim PvtTbl As PivotTable
    Dim ws As Worksheet
    Dim cht As Shape
    Dim rng As Range
    Set rng = ActiveSheet.Range("D2:K17")
    Set ws = Worksheets("Sheet3")
    Set PvtTbl = ws.PivotTables("CommentPivotTable")
    Set cht = ws.Shapes.AddChart2(Left:=rng.Left, Top:=rng.Top)

    With cht
        .Chart.SetSourceData PvtTbl.TableRange1
        .Chart.ChartTitle.Text = "Total Counts for Comments"
        .Chart.SetElement msoElementDataLabelOutSideEnd
    End With
    
    Set PvtTbl = Nothing
    Set cht = Nothing
    Set rng = Nothing
    
    Set rng = ActiveSheet.Range("D23:K36")
    Set PvtTbl = ws.PivotTables("HoldTbl")
    Set cht = ws.Shapes.AddChart2(Left:=rng.Left, Top:=rng.Top)

    With cht
        .Chart.SetSourceData PvtTbl.TableRange1
        .Chart.ChartTitle.Text = "Total Counts for Hold Code"
        .Chart.SetElement msoElementDataLabelOutSideEnd
    End With

End Sub
1 Answers

For the charts the source data is always the same :


.Chart.SetSourceData PvtTbl.TableRange1

which is always equal to :

PRange = DSheet.Cells(1, 1).Resize(lastrow, LastCol)

Try to create another sheet for your second pivot table, or set your source data for the second chart to A2:B31

Related