2015-10-02 68 views
1

以下工作正常,但是我希望目標工作表是特定的現有工作表"PIVOT"。我也想刪除rowscolumns的總計。有任何想法嗎? ColumnGrandRowGrand和:Excel VBA數據透視表嚮導,設置表格目標並刪除總計

Sub CreatePivot() 
' Creates a PivotTable report from the table on Sheet1 
' by using the PivotTableWizard method with the PivotFields 
' method to specify the fields in the PivotTable. 
Dim objTable As PivotTable, objField As PivotField 

' Select the sheet and first cell of the table that contains the data. 
ActiveWorkbook.Sheets("DATA").Select 
Range("A1").Select 

' Create the PivotTable object based on the Employee data on Sheet1. 
Set objTable = ActiveWorkbook.Sheets("DATA").PivotTableWizard 

' Specify row and column fields. 
Set objField = objTable.PivotFields("CCY") 
objField.Orientation = xlRowField 

Set objField = objTable.PivotFields("ACC") 
objField.Orientation = xlRowField 

Set objField = objTable.PivotFields("VD") 
objField.Orientation = xlColumnField 

' Specify a data field with its summary 
' function and format. 
Set objField = objTable.PivotFields("AMOUNT") 
objField.Orientation = xlDataField 
objField.Function = xlSum 
objField.NumberFormat = "#,##0" 

End Sub 

回答

0

目標片材可通過的PivotTableWizardTableDestination的參數來定義,那麼就可以用去除總計。這是更改的代碼:

Sub CreatePivot() 
' Creates a PivotTable report from the table on Sheet1 
' by using the PivotTableWizard method with the PivotFields 
' method to specify the fields in the PivotTable. 
Dim objTable As PivotTable, objField As PivotField 

' Select the sheet and first cell of the table that contains the data. 
ActiveWorkbook.Sheets("DATA").Select 
Range("A1").Select 

' Create the PivotTable object based on the Employee data on PIVOT with the name 'PivotTableName'. 
Set objTable = ActiveWorkbook.Sheets("DATA").PivotTableWizard(TableDestination:="PIVOT!R1C1", TableName:="PivotTableName") 

'Remove grand totals 
With Sheets("PIVOT").PivotTables("PivotTableName") 
    .ColumnGrand = False 
    .RowGrand = False 
End With 

' Specify row and column fields. 
Set objField = objTable.PivotFields("CCY") 
objField.Orientation = xlRowField 

Set objField = objTable.PivotFields("ACC") 
objField.Orientation = xlRowField 

Set objField = objTable.PivotFields("VD") 
objField.Orientation = xlColumnField 

' Specify a data field with its summary 
' function and format. 
Set objField = objTable.PivotFields("AMOUNT") 
objField.Orientation = xlDataField 
objField.Function = xlSum 
objField.NumberFormat = "#,##0" 

End Sub