数据透视表不显示小计

时间:2014-06-11 19:18:31

标签: vba excel-2010 pivot-table

我正在将表转换为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

我刚刚添加了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

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

 .PivotFields("Order #").Subtotals(1) = True
 .PivotFields("Order #").Subtotals(1) = False

6 个答案:

答案 0 :(得分:18)

全部,多亏了一些研究,我找到了解决方案

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

答案 1 :(得分:0)

.Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)

肯定会一次性将所有可能的小计设置为false。

答案 2 :(得分:0)

.PivotFields("Pivot Field Name").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)

它只是为了说清楚它不是更好

答案 3 :(得分:0)

我认为这更容易。

ActiveSheet.PivotTables("Supplier").RowGrand = False
ActiveSheet.PivotTables("Supplier").ColumnGrand = False

答案 4 :(得分:0)

另一种方法是在创建字段时将每个PivotField小计属性设置为false。这完全取决于您用来创建数据透视表的方法。如下所示,当您从使用CreatePivotTable()方法Set的数据透视缓存中使用Add方法时,该方法不起作用。

' Create the PivotTable object based on the Employee data on Sheet1.
Set objTable = Sheet1.PivotTableWizard
With objTable
    'Specify row and column fields.
    With .PivotFields("Supplier")
        .Subtotals(1) = False
        .Orientation = xlRowField
    End With
    With .PivotFields("Part #")
        .Subtotals(1) = False
        .Orientation = xlRowField
    End With
    With .PivotFields("Tracking #")
        .Subtotals(1) = False
        .Orientation = xlRowField
    End With
    With .PivotFields("Packing/Inv#")
        .Subtotals(1) = False
        .Orientation = xlRowField
    End With
    With .PivotFields("PO#")
        .Subtotals(1) = False
        .Orientation = xlRowField
    End With
    With .PivotFields("Ship Date")
        .Subtotals(1) = False
        .Orientation = xlRowField
    End With

    ' Specify a data field with its summary
    ' function and format.
    With .PivotFields("Qty")
        .Subtotals(1) = False
        .Orientation = xlDataField
        .Function = xlSum
    End With
End With

答案 5 :(得分:-1)

我发现我不需要将小计设置为' true'第一。将其直接设置为false很好。