我对Excel Pivot有一个问题,我现在一直在努力解决几个小时。我有一个Record Macro
下面的代码。它应该创建一个数据透视表并添加“记录类型”作为第一列,然后为每个记录类型的总记录添加另一列,但它删除第一列。我正在尝试这样做http://youtu.be/UseY0lEPu80但是当我使用VBA时,我遇到了这个问题http://youtu.be/eOxXX336gP0
S1LastRow = 569
S1LastColumn = 17
Sheet2.UsedRange.Delete
Application.Goto Sheet2.Range("A1"), True
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= "Main!R1C1:R" & S1LastRow & "C" & S1LastColumn, Version:=xlPivotTableVersion12).CreatePivotTable TableDestination:="Graph!R1C1", TableName:="Summary", DefaultVersion :=xlPivotTableVersion12
With ActiveSheet.PivotTables("Summary").PivotFields("Record Type")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("Summary").AddDataField ActiveSheet.PivotTables("Summary").PivotFields("Record Type"), "Count of Record Type", xlCount
With ActiveSheet.PivotTables("Summary")
.ColumnGrand = False
.RowGrand = False
End With
答案 0 :(得分:1)
这样做:
Dim PC As Excel.PivotCache
Dim PT As Excel.PivotTable
S1LastRow = 569
S1LastColumn = 17
Sheet2.UsedRange.Delete
Application.Goto Sheet2.Range("A1"), True
Set PC = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:="Main!R1C1:R" & S1LastRow & "C" & S1LastColumn, _
Version:=xlPivotTableVersion12)
Set PT = PC.CreatePivotTable(TableDestination:="Graph!R1C1", TableName:="Summary", _
DefaultVersion:=xlPivotTableVersion12)
With PT
' add the data field first if you want to use the same field as a row/column field too
.AddDataField .PivotFields("Record Type"), "Count of Record Type", xlCount
With .PivotFields("Record Type")
.Orientation = xlRowField
.Position = 1
End With
.ColumnGrand = False
.RowGrand = False
End With