Pivot Table Issues

How to Use VBA to Add and Sum PivotTable Data Fields in Excel

Huma Ashraf ChHuma Ashraf Ch Oct 21, 2026 870 views

Question details

The user needs to automate adding multiple fields to the PivotTable Values area and summarize them using the SUM function via a VBA macro without triggering runtime error 1004.

How to Use VBA to Add and Sum PivotTable Data Fields
Product
Excel
Device & OS
not provided
Scenario
Running a VBA script to loop through multiple PivotTables across different worksheets to bulk-add and sum data fields.
Observed behavior
The macro throws VBA runtime error 1004 when it encounters existing value fields with identical captions or when the code incorrectly relies on the ActiveSheet object during a loop.
Before you start

Before running the macro, manually check your PivotTables and remove any existing value fields (such as 'Sum of Fabric') that the VBA code might try to recreate, as duplicate captions will immediately trigger an error.

Solution 1Recommended

Use a Named Range and Correct Worksheet References in VBA

This solution prevents runtime error 1004 by explicitly referencing each worksheet instead of ActiveSheet and cleanly applying the xlSum function to a predefined list of fields.

When running a VBA loop across multiple PivotTables on different worksheets, using the 'ActiveSheet' reference can cause the code to apply changes to the wrong sheet or fail entirely. It is crucial to define a specific worksheet object.

To easily manage the fields you want to add, store the required field names in a named range on your spreadsheet rather than hardcoding them. You can then iterate through this range within your macro.

1
Define your field names in a named range

Create a named range in your workbook containing the exact text of the field names you want to add to the PivotTable Values area.

2
Set up the Worksheet and PivotTable loops

Open the VBA Editor (Alt + F11) and write a 'For Each' loop to iterate through each worksheet (`ws`) in your workbook. Inside that loop, create a nested loop to iterate through each PivotTable (`PT`) on the current `ws`.

3
Apply the AddDataField method

Inside the PivotTable loop, iterate through your named range and call the add data field method using this syntax: `PT.AddDataField Field:=PT.PivotFields(FN), Caption:="Sum of " & FN, Function:=xlSum`.

Use a Named Range and Correct Worksheet References in VBA
Clear Existing Fields: To make your script foolproof, add a line of code to loop through and remove `PT.DataFields` before adding the new ones. This guarantees no conflicting captions like 'Sum of FABRIC' exist.

Automate PivotTables and Macros with WPS Spreadsheet

WPS Spreadsheet provides powerful and native support for VBA macros, allowing you to seamlessly run your automated PivotTable scripts to add and sum data fields across multiple sheets without compatibility issues.

  1. 1. Open your macro-enabled workbook: Launch WPS Spreadsheet and open the .xlsm file containing your PivotTable VBA script.
  2. 2. Access the Developer tools: Navigate to the 'Developer' tab on the top ribbon. If it is hidden, enable it from the settings menu.
  3. 3. Run your PivotTable macro: Click 'Macros', select your specific PivotTable update script from the list, and click 'Run' to apply the SUM fields across all your designated sheets.
Native support for VBA and macro execution for complex automated workflows.Complete compatibility with Microsoft Excel (.xlsx, .xlsm, .xls) file formats.Advanced PivotTable features for deep data analysis and rapid summaries.Lightweight, fast to install, and free to download with a familiar ribbon UI.
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get runtime error 1004 when adding a PivotTable data field in VBA?

Runtime error 1004 usually occurs if your VBA code attempts to add a data field with a caption that already exists in the PivotTable (e.g., trying to add 'Sum of Fabric' when it is already there), or if your loop relies on 'ActiveSheet' while trying to modify a PivotTable on a background worksheet.

How can I force a PivotTable field function to SUM using VBA?

When adding the data field using the `AddDataField` method, you must explicitly set the function parameter. Use `Function:=xlSum` at the end of your syntax to ensure the field summarizes by sum rather than count.

How do I reference multiple field names efficiently in my VBA macro?

The most efficient and maintainable method is to store your required PivotTable field names in a named range on a worksheet. You can then use a `For Each` loop in your VBA script to iterate through that named range dynamically.