【发布时间】:2018-03-11 06:01:17
【问题描述】:
我创建了各种数据透视表,但仅在其他工作表(插入新工作表)中从数据中创建。现在,我在要创建数据透视表的同一个 Excel 表中拥有数据。它一直在说它在我设置 PCache 的那一行有问题。我在下面提供代码
Sub a()
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
Set PSheet = ActiveSheet
LastRow = PSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = PSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = PSheet.Cells(1, 1).Resize(LastRow, LastCol)
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable TableDestination:=PSheet.Cells(2, 13), TableName:="PivotTable1"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Destination")
.Orientation = xlRowField
.Position = 1
.Subtotals(1) = True
.Subtotals(1) = False
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Total trucks")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.Name = "Sum of Quantity (cases/sw)"
End With
'PTable.LayoutRowDefault = xlTabularRow
'Range("M2").Value = "Commodity Code"
End Sub
【问题讨论】:
标签: vba excel pivot-table