Update a Dynamic Excel Range and Refresh PivotTables with VBA
Question details
Create a range with a fixed number of rows and expanding columns, then refresh an Excel PivotTable and PivotChart from that range.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- New columns are added to a worksheet data source, and the user wants a dynamic range and VBA refresh process for the associated PivotTable and PivotChart.
- Observed behavior
- The source range should include the new columns while retaining its fixed row count, and the PivotTable and PivotChart should reflect the refreshed source.
Make sure the source has a header in every used column, with no gaps, and that the first column contains a header. Enable the Developer tab if needed, and save the workbook as an Excel Macro-Enabled Workbook (.xlsm) before adding VBA.
Create a Dynamic Named Range and Refresh PivotTables with VBA
Use a named range for a fixed row span and run a macro after adding source columns.
This example uses rows 2 through 15 on Sheet3, including the header row. The range expands as contiguous headers are added from column A through column Q. Change Q in the formula if data can extend farther.
Refreshing makes new source fields available in the PivotTable Fields list, but it does not automatically add them to the PivotTable layout.
In Excel, select Formulas > Name Manager > New. Name the range DynamicSource and enter =OFFSET(Sheet3!$A$2,0,0,14,COUNTA(Sheet3!$A$2:$Q$2)) in Refers to, then click OK.
Click the PivotTable, select PivotTable Analyze > Change Data Source, and enter DynamicSource in the Table/Range box. Click OK.
Open Developer > Visual Basic, select Insert > Module, and paste this code: Sub RefreshPivotsAndCharts() Dim ws As Worksheet Dim pt As PivotTable Dim co As ChartObject Application.Calculate For Each ws In ThisWorkbook.Worksheets For Each pt In ws.PivotTables pt.RefreshTable Next pt For Each co In ws.ChartObjects co.Chart.Refresh Next co Next ws End Sub
Add the new columns and their headers on Sheet3, then select Developer > Macros, choose RefreshPivotsAndCharts, and click Run. The macro refreshes all PivotTables and embedded charts in the workbook containing the code.
Click the PivotTable and use the PivotTable Fields pane to drag any new fields into Rows, Columns, Values, or Filters.

Work with PivotTable Source Data in WPS Spreadsheets
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for opening and editing Excel files. Its format compatibility helps you move everyday spreadsheet work between applications, although it does not fix Excel-specific VBA behavior.

Frequently Asked Questions
Why does the named range stop expanding after column Q?
The COUNTA portion of the example checks headers only through Sheet3!$Q$2. Open Formulas > Name Manager, edit DynamicSource, and extend that reference to a later column.
Will refreshing automatically show a newly added column in the PivotTable?
Refreshing makes the new column available as a field, but you may need to select the PivotTable and drag that field into the desired area of the PivotTable Fields pane.
How can I refresh the PivotTable without running VBA?
Click the PivotTable and select PivotTable Analyze > Refresh. The associated PivotChart updates when its PivotTable is refreshed.
Why does the formula use 14 for the range height?
The example covers rows 2 through 15, which is 14 rows including the header. Change the height argument in Formulas > Name Manager if your fixed row span is different.




