Excel VBA 数据透视表 Table 向导,设置 table 目标并删除总计
Excel VBA Pivot Table Wizard, set table destination and remove grand totals
下面的效果很好,但我希望目标 sheet 是一个特定的现有 sheet,名为 "PIVOT"
。另外,我想删除 rows
和 columns
的总计。有任何想法吗?
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
目标 sheet 可以由 PivotTableWizard
的 TableDestination
参数定义,然后您可以删除总计:ColumnGrand
和 RowGrand
.这是更改后的代码:
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
下面的效果很好,但我希望目标 sheet 是一个特定的现有 sheet,名为 "PIVOT"
。另外,我想删除 rows
和 columns
的总计。有任何想法吗?
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
目标 sheet 可以由 PivotTableWizard
的 TableDestination
参数定义,然后您可以删除总计:ColumnGrand
和 RowGrand
.这是更改后的代码:
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