Excel VBA创buildPivot,不出现表格

我下面的VBA代码试图创build一个基于“Sheet2”中的数据的数据透视表,在这张表上添加一个新的工作表“Pivot Table”并创build一个Pivot表。

但是,代码正在执行没有错误,只是没有表出现在新的工作表“数据透视表”,我不明白为什么。

此外,PT的Sheet2上的数据现在开始于B列,不确定这是否有效。

Dim PSheet As Worksheet Dim DSheet As Worksheet Dim PCache As PivotCache Dim PTable As PivotTable Dim PRange As Range Dim LastRow1 As Long Dim LastCol As Long 'Insert a New Blank Worksheet On Error Resume Next Application.DisplayAlerts = False Worksheets("PivotTable").Delete Sheets.Add Before:=ActiveSheet ActiveSheet.Name = "PivotTable" Application.DisplayAlerts = True Set PSheet = Worksheets("PivotTable") Set DSheet = Worksheets("Sheet2") '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) 'Define Pivot Cache Set PCache = ActiveWorkbook.PivotCaches.Create _ (SourceType:=xlDatabase, SourceData:=PRange). _ CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _ TableName:="MilestonePivotTable") 'Insert Blank Pivot Table Set PTable = PCache.CreatePivotTable _ (TableDestination:=PSheet.Cells(1, 1), TableName:="MilestonePivotTable") 'Insert Row Fields With ActiveSheet.PivotTables("MilestonePivotTable").PivotFields("Resource Name") .Orientation = xlRowField .Position = 1 End With With ActiveSheet.PivotTables("MilestonePivotTable").PivotFields("Deliverable") .Orientation = xlRowField .Position = 2 End With 'Insert Column Fields With ActiveSheet.PivotTables("MilestonePivotTable").PivotFields("Milestone Date") .Orientation = xlColumnField .Position = 1 End With 

在这里输入图像说明

只有半testing(由于只有虚拟数据),但我相信以下将解决所有的错误:

 Dim PSheet As Worksheet Dim DSheet As Worksheet Dim PCache As PivotCache Dim PTable As PivotTable Dim PRange As Range 'LastRow was not declared Dim LastRow As Long 'LastRow1 is not used 'Dim LastRow1 As Long Dim LastCol As Long 'Insert a New Blank Worksheet On Error Resume Next Application.DisplayAlerts = False Worksheets("PivotTable").Delete Sheets.Add Before:=ActiveSheet ActiveSheet.Name = "PivotTable" Application.DisplayAlerts = True 'Switch off error "masking" once you don't need it On Error GoTo 0 Set PSheet = Worksheets("PivotTable") Set DSheet = Worksheets("Sheet2") 'Define Data Range 'a) Base last row on column B if you don't have data in column A 'b) Use "DSheet.Rows.Count" and "DSheet.Columns.Count" - although not strictly ' necessary in this situation, it is a good practice to get into LastRow = DSheet.Cells(DSheet.Rows.Count, "B").End(xlUp).Row LastCol = DSheet.Cells(1, Dsheet.Columns.Count).End(xlToLeft).Column 'Don't include column A in your data range Set PRange = DSheet.Cells(1, "B").Resize(LastRow, LastCol - 1) 'Define Pivot Cache (not pivot table) Set PCache = ActiveWorkbook.PivotCaches.Create _ (SourceType:=xlDatabase, SourceData:=PRange) 'Set PCache = ActiveWorkbook.PivotCaches.Create _ '(SourceType:=xlDatabase, SourceData:=PRange). _ 'CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _ 'TableName:="MilestonePivotTable") 'Insert Blank Pivot Table Set PTable = PCache.CreatePivotTable _ (TableDestination:=PSheet.Cells(1, 1), TableName:="MilestonePivotTable") 'Use PSheet instead of ActiveSheet in all the following statements 'In fact, because the PTable object exists, we can just use it 'Insert Row Fields 'With AciveSheet.PivotTables("MilestonePivotTable").PivotFields("Resource Name") 'With PSheet.PivotTables("MilestonePivotTable").PivotFields("Resource Name") With PTable.PivotFields("Resource Name") .Orientation = xlRowField .Position = 1 End With With PTable.PivotFields("Deliverable") .Orientation = xlRowField .Position = 2 End With 'Insert Column Fields With PTable.PivotFields("Milestone Date") .Orientation = xlColumnField .Position = 1 End With