Этот код раньше работал в Office 365. Сейчас я работаю в Office 2016 и получаю сообщение об ошибке во время выполнения в последней строке. Входные данные для этого:
'Create a Pivot table called pt commission on sheet "Pivot Table" from the data on sheet2
Call ptSettings.CreatePivotTable("Pivot Table", Sheet2, "ptCommission")
Подставка, которая выбрасывает ошибку в последней строке:
Sub CreatePivotTable(ptSheet As String, dSheet As Worksheet, ptName As String)
'Creates a pivottable called ptName on sheets(ptSheet) with the data from dsheet
Dim ptCache As PivotCache
Dim ptTable As pivotTable
Dim ptRange As Range
Dim lastRow As Long
Dim lastCol As Long
Application.DisplayAlerts = False
Worksheets(ptSheet).Delete
Sheets.Add before:=ActiveSheet
ActiveSheet.Name = ptSheet
Application.DisplayAlerts = True
lastRow = dSheet.Cells(Rows.Count, 2).End(xlUp).Row
lastCol = dSheet.Cells(2, Columns.Count).End(xlToLeft).Column
Set ptRange = dSheet.Cells(1, 1).Resize(lastRow, lastCol + 1)
Set ptCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=ptRange)
Set ptTable = ptCache.CreatePivotTable _
(TableDestination:=Sheets(ptSheet).Cells(3, 1), TableName:=ptName)
End Sub