数据透视表不显示小计

ing*_*ala 4 vba pivot-table excel-2010

我正在将表转换为Pivot,然后转换为CSV文件.我想删除数据透视表的小计.到目前为止:这是我的进步.

MACRO CreatePivot:

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("Employees").Select
Range("A1").Select

' Create the PivotTable object based on the Employee data on Sheet1.
Set objTable = Sheet1.PivotTableWizard

' Specify row and column fields.
Set objField = objTable.PivotFields("Supplier")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Part #")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Tracking #")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Packing/Inv#")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("PO#")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Ship Date")
objField.Orientation = xlRowField

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


' Specify a page field.
Set objField = objTable.PivotFields("Carrier")
objField.Orientation = xlPageField


' Prompt the user whether to delete the PivotTable.
Application.DisplayAlerts = False
If MsgBox("Delete the PivotTable?", vbYesNo) = vbYes Then
    ActiveSheet.Delete
End If
Application.DisplayAlerts = True

End Sub
Run Code Online (Sandbox Code Playgroud)

我还添加了MACRO DesigningPivotTable:

Sub DesigningPivotTable()
Dim PT As PivotTable
Set PT = ActiveSheet.PivotTables(1)
PT.TableRange1.Select
With PT
 .NullString = 0
 .RepeatAllLabels Repeat:=xlRepeatLabels
 .ColumnGrand = False
 .RowGrand = 0
 .PivotFields("Order #").Subtotals(1) = True
 .PivotFields("Order #").Subtotals(1) = False
End With
End Sub
Run Code Online (Sandbox Code Playgroud)

根据Excel 2013 Pivot Table Data Crunching; 你可以打开第一个小计,这个方法会自动禁用所有其他小计,因此:它对我不起作用:

 .PivotFields("Order #").Subtotals(1) = True
 .PivotFields("Order #").Subtotals(1) = False
Run Code Online (Sandbox Code Playgroud)

ing*_*ala 20

所有这些,多亏了一些研究,我找到了解决方案

Sub PivotTableLayout2b()
Dim PvtTbl As PivotTable
Dim pvtFld As PivotField

Set PvtTbl = ActiveSheet.PivotTables(1)

'hide Subtotals for all fields in the PivotTable .

With PvtTbl
 For Each pvtFld In .PivotFields
 pvtFld.Subtotals(1) = True
 pvtFld.Subtotals(1) = False
Next pvtFld
End With
End Sub
Run Code Online (Sandbox Code Playgroud)

  • 评论,似乎如果您的数据透视表中有任何折叠字段,则可能会出错。我在“With PvtTbl”之后添加了“On Error Resume Next”来跳过这些。 (2认同)