VBA没有创建PivotCache

时间:2018-10-24 07:52:41

标签: excel vba

我已经编写了一个宏来创建数据透视表,但是未创建数据透视表,因为未创建数据透视表缓存。我似乎不知道为什么。有人可以告诉我我在做什么错吗?

Dim LastColumn As Long, LastRow As Long
Dim wsTO As Worksheet, wsEF As Worksheet
Dim wbTO As Workbook
Dim PCache As PivotCache, PTable As PivotTable
'Determine the data range you want to pivot
LastColumn = wsTO.Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = wsTO.Cells(Rows.Count, 1).End(xlUp).Row
Set PRange = wsTO.Cells(1, 1).Resize(LastRow, LastColumn)

'Set the pivot cache
Set PCache = wbTO.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=wsEF.Cells(10, 7), _
TableName:="PostOccupationTable")
'Create a blank pivot table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=wsEF.Cells(10, 7), TableName:="PostOccupationTable")
'Insert Row & Column Fields
 With wsEF.PivotTables("PostOccupationTable").PivotFields("IndCategory2")
    .Orientation = xlRowField
    .Position = 1
End With
With wsEF.PivotTables("PostOccupationTable").PivotFields("PostCategory2")
    .Orientation = xlColumnField
    .Position = 1
End With
'Insert data field
wsEF.PivotTables("PostOccupationTable").AddDataField ActiveSheet.PivotTables( _
    "PostOccupationTable").PivotFields("NAME"), "Count of NAME", xlCount
'Set filters
With wsEF.PivotTables("PostOccupationTable").PivotFields("PostCategory2")
    .PivotItems("(blank)").Visible = False
End With
With wsEF.PivotTables("PostOccupationTable").PivotFields("IndCategory2")
    .PivotItems("(blank)").Visible = False
End With
'Apply Style
wsEF.PivotTables("PostOccupationTable").TableStyle2 = _
    "PivotStyleMedium2"

1 个答案:

答案 0 :(得分:0)

在其他论坛上搜索时,我发现问题与excel并不总是接受设置的范围(尽管应该)有关。 我已经使用R1C1引用样式重写了将范围编码为STRING的代码,从而解决了我的问题。现在创建缓存,从而创建数据透视表。完整的工作代码如下。

Dim LastColumn As Long, LastRow As Long
Dim wsTO As Worksheet, wsEF As Worksheet
Dim wbTO As Workbook
Dim PCache As PivotCache, PTable As PivotTable
Dim PivotRange As String

Set wbEFiche = Workbooks("ThisWorkbook.xlsm")
Set wsEF = wbEFiche.Worksheets("Sheet1")
Set wsTO = wbTO.Sheets("Sheet2")

LastColumn = wsTO.Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = wsTO.Cells(Rows.Count, 1).End(xlUp).Row
'Due to an excel bug, the range has to be encoded in a string format, using a R1C1 reference style
PivotRange = wsTO.Name & "!" & wsTO.Range(wsTO.Cells(1, 1), wsTO.Cells(LastRow, LastColumn)).Address(ReferenceStyle:=xlA1)
'Set the pivot cache
Set PCache = wbTO.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PivotRange). _
CreatePivotTable(TableDestination:=wsEF.Cells(10, 7), _
TableName:="PostOccupationTable")
'Create a blank pivot table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=wsEF.Cells(10, 7), TableName:="PostOccupationTable")
'Insert Row & Column Fields
 With wsEF.PivotTables("PostOccupationTable").PivotFields("IndCategory2")
    .Orientation = xlRowField
    .Position = 1
End With
With wsEF.PivotTables("PostOccupationTable").PivotFields("PostCategory2")
    .Orientation = xlColumnField
    .Position = 1
End With
'Insert data field
wsEF.PivotTables("PostOccupationTable").AddDataField ActiveSheet.PivotTables( _
    "PostOccupationTable").PivotFields("NAME"), "Count of NAME", xlCount
'Set filters
With wsEF.PivotTables("PostOccupationTable").PivotFields("PostCategory2")
    .PivotItems("(blank)").Visible = False
End With
With wsEF.PivotTables("PostOccupationTable").PivotFields("IndCategory2")
    .PivotItems("(blank)").Visible = False
End With
'Apply Style
wsEF.PivotTables("PostOccupationTable").TableStyle2 = _
    "PivotStyleMedium2"