Я построил следующий код VBA, который автоматически создает таблицу PIVOT и диаграммы на новом листе.
Сводная таблица в порядке. Гистограмма тоже хорошо. Однако круговая диаграмма (диаграмма 2) выбирает данные «закрытого» столбца (столбец 1 в сводной таблице) из сводной таблицы. Я хочу, чтобы он выбрал итоговый столбец, который является столбцом 3 в PT.
Я пытался изменить диапазон в верхней части кода, но это не сработало. Я также удалил столбчатую диаграмму и выполнил ее только с PT и круговой диаграммой, и все та же проблема (данные считываются только из столбца «Закрыто»).
Вложение: содержит снимок экрана с выводом.

Вот мой код ниже:
Sub PT_C_RiskType_Status()
'Defining the variables
Dim pc As PivotCache
Dim ws As Worksheet
Dim pt As PivotTable
Dim sh As Shape
Dim sh1 As Shape
Dim ch As Chart
Dim ch1 As Chart
Set pc = ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:="DataSource", _
Version:=xlPivotTableVersion15)
Set ws = Sheets.Add
ws.Select
Cells(3, 1).Select
Range("A3").Select
'Create the pivot table
Set pt = pc.CreatePivotTable(TableDestination:=ws.Name & "!R3C1", TableName:="Pivottable")
'Adding the data fields
pt.AddDataField _
Field:=pt.PivotFields("Status"), _
Function:=XlConsolidationFunction.xlCount
'Adding the fields
pt.AddFields _
RowFields:="Risk Type", _
ColumnFields:="Status", _
PageFields:=Array("Date Received", "Risk Level")
'Adding formatting options
pt.DataFields(1).NumberFormat = "0"
'Adding Filters for the pivot
With ActiveSheet.PivotTables("PivotTable").PivotFields("Date Received")
.PivotItems("(blank)").Visible = False
End With
ActiveSheet.PivotTables("PivotTable").PivotFields("Date Received"). _
EnableMultiplePageItems = True
With ActiveSheet.PivotTables("PivotTable").PivotFields("Risk Level")
.PivotItems("N/A").Visible = False
End With
ActiveSheet.PivotTables("PivotTable").PivotFields("Risk Level"). _
EnableMultiplePageItems = True
With ActiveSheet.PivotTables("PivotTable").PivotFields("Risk Type")
.PivotItems("(blank)").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Status")
.PivotItems("(blank)").Visible = False
End With
'Adding chart
Set sh = ws.Shapes.AddChart2( _
XlChartType:=XlChartType.xlColumnStacked, _
Width:=350, Height:=300)
Set sh1 = ws.Shapes.AddChart2( _
XlChartType:=XlChartType.xlPie, _
Width:=200, Height:=200)
Set ch1 = sh1.Chart
Set ch = sh.Chart
'The position of the chart (the Column Stacked Chart)
ch.SetSourceData pt.TableRange1
sh.Top = pt.TableRange1.Top
sh.Left = pt.TableRange1.Left + pt.TableRange1.Width + 10
'The position of the chart1 (The Pie Chart)
sh1.Left = sh.Left - sh.Width
'Formatting for chart and chart1
'Colors the area for "Opened" status
ch.FullSeriesCollection(2).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent4
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0.400000006
.Transparency = 0
.Solid
End With
'Colors the area for "Closed" status
ch.FullSeriesCollection(1).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent3
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.25
.Transparency = 0
.Solid
End With
'Formatting the labels inside the bars
'Adding data labels to the bars
ch.ApplyDataLabels (xlDataLabelsShowValue)
'Formatting Chart1 (Pie Chart)
ch1.ChartTitle.Select
Selection.Delete 'Delete the title
'Changing the color of the second portion of the chart
ch1.FullSeriesCollection(1).Select
ch1.FullSeriesCollection(1).Points(2).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent4
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0.400000006
.Transparency = 0
.Solid
End With
' Moving the chart to the bottom of the pivot table
ActiveSheet.Shapes("Chart 2").IncrementLeft 2.5
ActiveSheet.Shapes("Chart 2").IncrementTop 97.5
End Sub
Пожалуйста, сообщите.
Спасибо.