Excel VBA Pivot Table Remove All Subtotals Guide
In this article
To remove all subtotals from a Pivot Table using Excel VBA, you must iterate through every PivotField in the PivotTable object and set the Subtotals property to Array(False). Because subtotals are managed on a per-field basis rather than as a single global toggle, a simple loop is the only way to ensure every row field is cleaned of automatic totals.
What You'll Need
- Microsoft Excel installed on a Windows PC (VBA is not supported in the web version of Excel).
- A workbook containing at least one existing Pivot Table.
- The Developer tab enabled in the Excel Ribbon.
- Basic familiarity with opening the Visual Basic Editor (VBE).
Step-by-Step
- Open the Visual Basic Editor: Open your Excel workbook. Press
Alt + F11on your keyboard. This keyboard shortcut opens the VBE window where all your macros and modules are stored. - Insert a New Module: In the VBE menu, click on
Insertand then selectModule. This creates a blank workspace where you can paste the code. Writing code in a module ensures the macro is available to the entire workbook rather than being tied to a specific sheet. - Define the Subroutine: Type the following line to start your macro:
Sub RemovePivotSubtotals(). This names the procedure so you can find it later in the Macro dialog box. - Declare Variables: To make the code efficient and readable, declare your objects. Add the following lines:
Dim pt As PivotTableDim pf As PivotField
These variables tell Excel thatptwill represent the Pivot Table andpfwill represent the individual fields within that table. - Set the Pivot Table Reference: You need to tell VBA which Pivot Table to target. If you only have one table on the active sheet, use:
Set pt = ActiveSheet.PivotTables(1)
Note that labels and index numbers may differ slightly depending on your Excel version; using(1)targets the first Pivot Table created on that specific sheet. - Create the Loop: Since subtotals are an attribute of the PivotField, you must loop through all fields. Add this line:
For Each pf In pt.PivotFields
This tells Excel to look at every single field, including those in the filters, columns, and rows. - Apply the Subtotal Removal: Inside the loop, add the command to disable subtotals. Use:
pf.Subtotals = Array(False)
TheSubtotalsproperty requires an array because Excel allows different types of subtotals (Automatic, Customized, etc.). PassingArray(False)effectively turns off all possible subtotal options for that field. - Close the Loop: End the loop by typing
Next pf. This moves the process to the next field until every field has been processed. - End the Subroutine: Type
End Subto complete the code block. - Run the Macro: Return to your Excel sheet. Press
Alt + F8, selectRemovePivotSubtotalsfrom the list, and clickRun. All subtotals in the targeted Pivot Table will vanish instantly.
Common Mistakes
One of the most frequent errors is attempting to set pt.Subtotals = False. This will result in a Run-time error '438': Object doesn't support this property or method. The PivotTable object itself does not have a global subtotal property; only the PivotFields within the table do. You must use a loop to address each field individually.
Another common mistake is failing to specify the correct sheet. If you use ActiveSheet, but your cursor is on a blank sheet or a data sheet when you run the macro, the code will crash because it cannot find a Pivot Table. Always ensure the sheet containing the table is selected, or replace ActiveSheet with Sheets("YourSheetName") for better stability.
Users also often forget that the Subtotals property expects an array. Writing pf.Subtotals = False (without the Array() wrapper) can cause a type mismatch error in certain versions of Excel. The Array(False) syntax is the standard way to communicate to the Pivot Table engine that no subtotaling should occur for any of the available subtotal types.
Tips
To make your code more robust, you can add an On Error Resume Next statement before the loop. Some PivotFields (like those used only as Page/Filter fields) may not support the Subtotals property. Adding this line prevents the macro from stopping with an error when it encounters a field that cannot have subtotals.
If you have multiple Pivot Tables on one sheet and want to clear all of them, wrap the entire logic in another loop: For Each pt In ActiveSheet.PivotTables. This ensures that no matter how many reports you have on a single dashboard, they all get a uniform, clean look without the clutter of subtotals.
For those who want to toggle subtotals back on, you can create a second macro that uses pf.Subtotals = Array(1). This restores the default automatic subtotals for every field in the table, allowing you to switch between a summarized view and a clean data view quickly.
Troubleshooting
If the macro runs but the subtotals are still visible, check if your Pivot Table is using the Data Model (Power Pivot). Pivot Tables created using the "Add this data to the Data Model" checkbox behave differently. In some cases, the standard PivotField object is replaced by a CubeField, which does not use the same Subtotals property. If you are using the Data Model, you may need to manually disable subtotals via the Design tab in the Ribbon.
If you receive a "Subscript out of range" error, it means ActiveSheet.PivotTables(1) cannot find a table. Verify that the table actually exists on the sheet you are currently viewing. If the table was renamed, you can try referencing it by its specific name: Set pt = ActiveSheet.PivotTables("PivotTable1").
If the VBE window is frozen or the macro is taking too long to run on a massive dataset, add Application.ScreenUpdating = False at the beginning of your code and Application.ScreenUpdating = True at the end. This prevents Excel from trying to redraw the screen every time a field is updated, which significantly increases execution speed.
Frequently Asked Questions
Can I remove subtotals for only one specific field?
Yes. Instead of using a For Each loop, you can target a specific field by name: pt.PivotFields("YourFieldName").Subtotals = Array(False). This allows you to keep subtotals for your primary categories while removing them for secondary ones.
Does this remove Grand Totals as well?
No. Subtotals and Grand Totals are different properties. To remove Grand Totals, you must use pt.ColumnGrand = False and pt.RowGrand = False. These are properties of the PivotTable object, not the PivotField object.
Will this macro work on a Pivot Table created from an external SQL database?
Yes, as long as the data is loaded into a standard Excel Pivot Table. The VBA properties for PivotFields apply to the table's presentation layer, regardless of whether the source is a local range, a CSV, or an external SQL server.
Is there a way to do this without VBA?
Yes. You can go to the Design tab on the Ribbon, click Subtotals in the Layout group, and select Do Not Show Subtotals. However, this often only applies to the current field or requires manual repetition for every field in the table, which is why VBA is preferred for complex tables.
Can I include this in a larger automation script?
Absolutely. You can turn this logic into a separate procedure and call it from a main macro using Call RemovePivotSubtotals. This is a common practice when building automated monthly reporting tools where data is refreshed and formatted automatically.