【问题标题】:PivotTable Do not show subtotals数据透视表不显示小计
【发布时间】:2014-08-01 23:32:52
【问题描述】:

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

宏创建枢轴:

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 数据透视表数据处理;您可以打开第一个小计,此方法会自动禁用所有其他小计,因此:它对我不起作用:

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

【问题讨论】:

    标签: vba excel-2010 pivot-table


    【解决方案1】:

    所有,感谢更多研究,我找到了解决方案

    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
    

    【讨论】:

    • 评论,如果您在数据透视表中有任何折叠字段,您可能会出错。我在With PvtTbl 之后添加了On Error Resume Next 以跳过这些。
    【解决方案2】:
    .PivotFields("Pivot Field Name").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
    

    不是说清楚就好了

    【讨论】:

      【解决方案3】:
      .Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
      

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

      【讨论】:

        【解决方案4】:

        我发现我不需要先将小计设置为“true”。将其直接设置为 false 很好。示例如下:

        With PvtTbl
            For Each pvtFld In .PivotFields
                pvtFld.Subtotals(1) = False
            Next pvtFld
        End With
        

        【讨论】:

          【解决方案5】:

          我认为这更容易。

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

          【讨论】:

            【解决方案6】:

            另一种方法是在创建字段时将每个 PivotField Subtotal 属性设置为 false。这完全取决于您用来创建数据透视表的方法。当您使用 Set 使用 Add 方法的 Pivot Cache 中的 CreatePivotTable() 方法时,以下方法不起作用。

            ' 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
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-11-03
              • 2013-03-12
              • 2014-11-01
              • 2015-09-18
              • 2017-05-13
              • 2013-07-13
              • 2021-02-12
              相关资源
              最近更新 更多