How to Use VBA to Add and Sum PivotTable Data Fields in Excel
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.

- 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 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.
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.
Create a named range in your workbook containing the exact text of the field names you want to add to the PivotTable Values area.
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`.
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`.

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. Open your macro-enabled workbook: Launch WPS Spreadsheet and open the .xlsm file containing your PivotTable VBA script.
- 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. 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.

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.




