[英]VBA: Create multiple pivot charts from multiple pivot tables in 1 sheet [Error: Method 'SetSourceData' of object '_chart' failed]
[英]How to create multiple pivot chart based on different pivot tables on the same excel sheet?
我也想在同一張工作表上使用不同的數據透視表將多個圖表添加到同一張工作表中。 但是,每當我嘗試在同一張表上制作第二張圖表時,它只會顯示第一個數據透視表中的數據。 我嘗試了許多不同的方法來修改代碼,但無法這樣做。 為什么會這樣以及對這個問題有什么建議?
這就是我運行代碼時發生的情況,第二個圖表未更新到第二個數據透視表
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
對於圖表,源數據始終相同:
.Chart.SetSourceData PvtTbl.TableRange1
這總是等於:
PRange = DSheet.Cells(1, 1).Resize(lastrow, LastCol)
嘗試為第二個數據透視表創建另一個工作表,或將第二個圖表的源數據設置為 A2:B31
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.