Я написал макрос для создания сводной диаграммы в Excel в одной книге.Если я запускаю макрос из той же книги, он добавляет правильную сводную диаграмму без каких-либо ошибок.Но если я запускаю макрос из другой рабочей книги, я получаю сообщение об ошибке «Определено приложение» или «Объект» в следующей строке.
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange, Version:=xlPivotTableVersion15).CreatePivotTable TableDestination:=DSheet.Cells(Table1_Start_Line, Column_Line), TableName:="HSDPivotTable", DefaultVersion:=xlPivotTableVersion15
Мой полный код ниже
Sub InsertPivotTable()
'Macro By ExcelChamps
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PTable1 As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
Dim Table1_Row_line As Integer
Dim Table1_Column_line As Integer
Dim Table2_Row_line As Integer
Dim Table2_Column_line As Integer
Dim Chart1_Row_Line As Integer
Dim Chart1_Column_Line As Integer
Dim Chart2_Row_Line As Integer
Dim Chart2_Column_Line As Integer
'Insert a New Blank Worksheet
'On Error Resume Next
Set DSheet = Worksheets("Sheet1")
Worksheets("Sheet1").Activate
'Define Data Range
LastRow = DSheet.Cells(DSheet.Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, DSheet.Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
Table1_Start_Line = 2
Table1_End_Line = Table1_Start_Line + LastRow
Column_Line = LastCol + 2
Table2_Start_Line = Table1_End_Line + 2
Table2_End_Line = Table2_Start_Line + LastRow
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange, Version:=xlPivotTableVersion15).CreatePivotTable TableDestination:=DSheet.Cells(Table1_Start_Line, Column_Line), TableName:="PivotTable", DefaultVersion:=xlPivotTableVersion15
Sheets("Sheet1").Select
DSheet.Cells(Table1_Start_Line, Column_Line).Select
With ActiveSheet.PivotTables("PivotTable").PivotFields("WW")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable").AddDataField ActiveSheet.PivotTables("PivotTable").PivotFields("Actual"), "Actual ", xlSum
ActiveSheet.PivotTables("PivotTable").AddDataField ActiveSheet.PivotTables("PivotTable").PivotFields("Actual(cumulative)"), "Actual (cumulative) ", xlSum
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$Z$" & Table1_Start_Line & ":$AB$" & Table1_End_Line)
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = "Chart1"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Chart1"
ActiveWorkbook.Worksheets("Sheet1").PivotTables("PivotTable").PivotCache.CreatePivotTable TableDestination:=DSheet.Cells(Table2_Start_Line, Column_Line), TableName:="IncomingVsCompletion", DefaultVersion:=xlPivotTableVersion15
Sheets("Sheet1").Select
Cells(Table2_Start_Line, Column_Line).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("IncomingVsCompletion").PivotFields("WW")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("IncomingVsCompletion").AddDataField ActiveSheet.PivotTables("IncomingVsCompletion").PivotFields("Plan"), "Plan ", xlSum
ActiveSheet.PivotTables("IncomingVsCompletion").AddDataField ActiveSheet.PivotTables("IncomingVsCompletion").PivotFields("Plan (cumulative)"), "Plan (cumulative) ", xlSum
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$Z$" & Table2_Start_Line & ":$AB$" & Table2_End_Line)
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = "Chart2"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Chart2"
ActiveWorkbook.ShowPivotTableFieldList = False
End Sub
Данные в разных книгах Excel будут разными.Я хочу создать сводную диаграмму в разных книгах.