VBA向下移动单元格,将枢轴应用于活动工作表

时间:2017-08-07 16:38:13

标签: excel vba excel-vba pivot-table

我录制了这个宏,将所有数据向下移动了75行,选择了我拥有的大量数据(行数变化,但列数是固定的),并在同一张表的A76处插入一个数据透视表(具有特定的一组)数据透视表格式化)。它适用于第一张工作表,但由于后续工作表上的工作表引用/单元格引用而继续出错。对此有任何帮助都很棒,谢谢!

Sub PivotCurrentSheet()

'shift down cells
Rows("1:75").Select
Selection.Insert Shift:=xlDown
Range("A76").Select

'create pivot table
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "Sheet1!R76C1:R86C22", Version:=6).CreatePivotTable TableDestination:= _
    "Sheet1!R1C1", TableName:="PivotTable1", DefaultVersion:=6
Sheets("Sheet1").Select
Cells(1, 1).Select

'column selections
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Account No")
    .Orientation = xlColumnField
    .Position = 1
End With

'data selections
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("Txn Amount"), "Sum of Txn Amount", xlSum

'row selections
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Opportunity")
    .Orientation = xlRowField
    .Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sales Order Number")
    .Orientation = xlRowField
    .Position = 2
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Project ID")
    .Orientation = xlRowField
    .Position = 3
End With

'No subtotals
ActiveSheet.PivotTables("PivotTable1").PivotFields("Batch Posting Date"). _
    Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
    False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Entry Date").Subtotals = _
    Array(False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Batch Description"). _
    Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
    False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Memo").Subtotals = Array( _
    False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Document Reference"). _
    Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
    False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Document Number"). _
    Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
    False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Customer ID").Subtotals = _
    Array(False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Journal").Subtotals = Array _
    (False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Account No").Subtotals = _
    Array(False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Txn Amount").Subtotals = _
    Array(False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Transaction Currency"). _
    Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
    False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Debit Amount").Subtotals = _
    Array(False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Credit Amount").Subtotals _
    = Array(False, False, False, False, False, False, False, False, False, False, False, False _
    )
ActiveSheet.PivotTables("PivotTable1").PivotFields("Line No").Subtotals = Array _
    (False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Item ID").Subtotals = Array _
    (False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Location Name").Subtotals _
    = Array(False, False, False, False, False, False, False, False, False, False, False, False _
    )
ActiveSheet.PivotTables("PivotTable1").PivotFields("Department ID").Subtotals _
    = Array(False, False, False, False, False, False, False, False, False, False, False, False _
    )
ActiveSheet.PivotTables("PivotTable1").PivotFields("Record Type").Subtotals = _
    Array(False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Customer Name").Subtotals _
    = Array(False, False, False, False, False, False, False, False, False, False, False, False _
    )
ActiveSheet.PivotTables("PivotTable1").PivotFields("Opportunity").Subtotals = _
    Array(False, False, False, False, False, False, False, False, False, False, False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Sales Order Number"). _
    Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
    False, False)
ActiveSheet.PivotTables("PivotTable1").PivotFields("Project ID").Subtotals = _
    Array(False, False, False, False, False, False, False, False, False, False, False, False)

'repeat item labels
ActiveSheet.PivotTables("PivotTable1").RepeatAllLabels xlRepeatLabels

'tabular format
ActiveSheet.PivotTables("PivotTable1").RowAxisLayout xlTabularRow  End Sub

0 个答案:

没有答案