Excel VBA创build枢轴不完全工作

Excel Pivot有一个问题,我一直在努力解决几个小时。 我有我从做一个Record Macro得到的代码。 它应该创build一个Pivot表,并添加“loggingtypes”作为第一列,然后为每个loggingtypes的总logging添加另一列,但删除第一列。 我试图做到这一点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 

像这样做:

 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