How to Create an Excel VBA Macro to Group Data, Refresh Pivot Tables, and Export to PDF
Question details
The user wants to automate an Excel workbook using VBA macros to generate unique IDs, refresh a pivot table, copy the grouped data and totals to a summary sheet, and export the result as a PDF.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating repetitive spreadsheet tasks including data manipulation, pivot table updates, and PDF reporting via VBA.
- Observed behavior
- The user needs a reliable macro solution to handle dynamic row ranges, refresh pivot data accurately, and successfully output a formatted PDF summary.
Before running or writing VBA macros, ensure you have saved a backup of your workbook as a Macro-Enabled Workbook (.xlsm) and have the Developer tab enabled in your Excel ribbon.
Modular VBA Approach: Separate Macros for Data Processing and PDF Export
Using separate macros makes the process easier to debug and manage. One macro handles data preparation and pivot refreshing, while the second handles exporting and emailing.
Breaking your automation into distinct modules prevents complex errors. The first macro will populate unique IDs in Column A, refresh the pivot cache, and transfer the totals. The second macro will strictly handle rendering the Summary sheet to a PDF format and triggering the Outlook email attachment.
Open the VBA editor by pressing Alt + F11, insert a new module, and write a script to populate Column A with unique IDs based on your data in Columns E and F.
Within the same script, update the pivot cache using dynamic row ranges to ensure new entries are captured, then instruct the macro to copy the resulting grouped data and totals to your 'Summary' sheet.
In a separate subroutine, write the code to export the 'Summary' sheet using the ExportAsFixedFormat Type:=xlTypePDF method, and optionally use Outlook Interop to attach the generated PDF to an email.
Insert shapes or Form Controls onto your worksheet, right-click them, and select 'Assign Macro' to link your newly created scripts for easy one-click automation.

Direct Data Transfer to Summary Sheet (Bypassing Pivot Tables)
If your pivot source frequently fails to include new rows, you can use VBA to directly group and transfer data from the Data sheet to the Summary sheet.
Automate Your Spreadsheets with WPS Office Spreadsheets
WPS Office offers a powerful spreadsheet editor that fully supports VBA macros, allowing you to seamlessly run your existing automation scripts to group data, refresh pivot tables, and export PDFs.
- 1. Open Your Workbook: Launch WPS Spreadsheets and open your existing macro-enabled workbook (.xlsm).
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon to access the VBA editor and macro settings.
- 3. Run Your Macros: Click 'Macros' to view, run, or edit your scripts for grouping data, refreshing pivot tables, and exporting PDFs.
- 4. Alternative Manual Export: If you prefer not to use VBA for exporting, simply click the built-in 'Export to PDF' tool for quick, manual PDF creation.

Frequently Asked Questions
Why is my VBA macro not capturing new rows in the pivot table?
This usually happens when the pivot table's data source is set to a fixed range (e.g., A1:F100). To fix this, define your data source as an Excel Table (Ctrl+T) or use a dynamic named range in your VBA code.
How can I automatically email the exported PDF using VBA?
You can automate emails by utilizing the Outlook Object Library in your VBA editor. Create an 'Outlook.Application' object, generate a new mail item, attach the saved PDF path using the .Attachments.Add method, and display or send the email.
Can I assign multiple macros to a single button click?
Yes. You can create a 'Master' macro that simply calls the other macros in sequence (e.g., 'Call ProcessData', then 'Call RefreshPivot', then 'Call ExportPDF'), and assign that master macro to your button.
What is the specific VBA code to export a worksheet as a PDF?
You can use the ExportAsFixedFormat method. A basic example looks like this: Sheets("Summary").ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\path\to\your\file.pdf".




