Use Excel VBA to Refresh, Filter, and Export PivotTable Data
Question details
Automate a monthly PivotTable refresh, company filter, and export to a new workbook.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A user needs to produce a company-specific sales export each month.
- Observed behavior
- The desired result is a refreshed PivotTable filtered to the selected company and saved separately without including another company's results.
Confirm that the PivotTable uses an up-to-date Excel table as its source and that Company is in the Filters area. Save the macro workbook as an .xlsm file, and note the worksheet name, PivotTable name, and exact company name.
Refresh and export a company-filtered PivotTable with VBA
Use this macro to save the displayed PivotTable results for one company as values in a new workbook.
This example assumes a standard, non-Data Model PivotTable sourced from a local Excel table. It refreshes the PivotTable cache but does not refresh external queries; complete those updates before running it.
The export contains summarized PivotTable results, including any displayed totals, not the individual source records. The macro clears existing PivotTable filters before applying the company filter.
Add the monthly sales rows to the Excel source table. In the PivotTable Fields pane, place the Company field in Filters. Check the PivotTable name under PivotTable Analyze > PivotTable Name.
Open the VBA editor, choose Insert > Module, and paste this code: Sub ExportCompanyPivot() Dim pt As PivotTable Dim pf As PivotField Dim outBook As Workbook Dim outSheet As Worksheet Dim resultRange As Range Dim companyName As String Dim outputPath As String Dim oldScreenUpdating As Boolean Dim errorMessage As String oldScreenUpdating = Application.ScreenUpdating On Error GoTo Failed Application.ScreenUpdating = False companyName = "Example Company" If ThisWorkbook.Path = "" Then Err.Raise vbObjectError + 1, , "Save this workbook first." outputPath = ThisWorkbook.Path & Application.PathSeparator & "Sales_Export.xlsx" If Dir(outputPath) <> "" Then Err.Raise vbObjectError + 2, , "The export file already exists. Rename or move it before running again." Set pt = ThisWorkbook.Worksheets("Pivot").PivotTables("PivotTable1") Set pf = pt.PivotFields("Company") If pf.Orientation <> xlPageField Then Err.Raise vbObjectError + 3, , "Put Company in the PivotTable Filters area." pt.PivotCache.Refresh pt.ClearAllFilters pf.CurrentPage = companyName Set resultRange = pt.TableRange1 Set outBook = Workbooks.Add(xlWBATWorksheet) Set outSheet = outBook.Worksheets(1) outSheet.Range("A1").Value = "Company: " & companyName outSheet.Range("A3").Resize(resultRange.Rows.Count, resultRange.Columns.Count).Value = resultRange.Value outSheet.Columns.AutoFit outBook.SaveAs Filename:=outputPath, FileFormat:=xlOpenXMLWorkbook Set outBook = Nothing Application.ScreenUpdating = oldScreenUpdating MsgBox "Export saved to: " & outputPath, vbInformation Exit Sub Failed: errorMessage = Err.Description On Error Resume Next If Not outBook Is Nothing Then outBook.Close SaveChanges:=False Application.ScreenUpdating = oldScreenUpdating On Error GoTo 0 MsgBox "Export stopped: " & errorMessage, vbExclamation End Sub
Replace Example Company, Pivot, PivotTable1, and Company in the code with your exact names. Save the workbook, then use Developer > Macros, select ExportCompanyPivot, and click Run.
Open Sales_Export.xlsx from the macro workbook's folder. Check the company label, figures, and totals before sharing it. Rename or move the file before the next run so it is not overwritten.

Export underlying records from a filtered PivotTable
Use this manual method when you need individual source rows rather than summarized PivotTable values.
Work with PivotTable workbooks in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easier to move everyday workbook tasks. The Excel VBA macro above is specific to Excel and should not be assumed to run unchanged in WPS.

Frequently Asked Questions
Does the VBA export include individual sales transactions?
No. The example copies the displayed PivotTable results as values. To retrieve underlying rows, double-click the relevant PivotTable value cell and inspect the detail sheet Excel creates.
How do I change the company being exported?
In the VBA editor, change the companyName value to the exact company item shown in the PivotTable's Company filter, then run ExportCompanyPivot again.
Why does the macro stop when the export file already exists?
It checks for Sales_Export.xlsx in the macro workbook's folder to avoid overwriting an earlier export. Rename or move the existing file before running it again.
Will refreshing the PivotTable update an external query first?
Not necessarily. This example refreshes the PivotTable cache, not external queries. Refresh and wait for external data to finish updating before running the macro.
Why can't the macro find the Company field?
Check the field's exact name in the PivotTable Fields pane and update the code if needed. The example also requires Company to be in the Filters area.




