我创建了一个宏来构建数据透视表。到目前为止,它给出了一个错误,指出对象PivotCache的Method CreatePivotTable失败了。问题肯定是PRange,因为当我尝试使用定义的范围运行它时它运行正常。有效的定义范围如下:
Set PRange = DSheet.Range(Cells(4, 61), Cells(44, 69))
我尝试运行的代码如下:
Dim DSheet As Worksheet
Dim PCache1 As PivotCache
Dim PTable1 As PivotTable
Dim PRange As Range
Dim LastRow As Long
Application.DisplayAlerts = True
Set DSheet = Worksheets("OTD")
LastRow = DSheet.Cells(Rows.Count, 62).End(xlUp).Row
Set PRange = DSheet.Cells(4, 61).Resize(LastRow, 69)
Set PCache1 = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)
Set PTable1 = PCache1.CreatePivotTable(TableDestination:=DSheet.Cells(1, 130), TableName:="PivotTable1")
With PTable1
.CompactLayoutRowHeader = "Customer"
With .PivotFields("Customer:")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Customer:")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
End With
End With