Pivot Table Issues

Update a Dynamic Excel Range and Refresh PivotTables with VBA

Emma BrownEmma Brown Nov 25, 2026 869 views

Question details

Create a range with a fixed number of rows and expanding columns, then refresh an Excel PivotTable and PivotChart from that range.

Update a Dynamic Excel Range and Refresh PivotTables with VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Define the source range

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.

2
Assign the PivotTable source

Click the PivotTable, select PivotTable Analyze > Change Data Source, and enter DynamicSource in the Table/Range box. Click OK.

3
Add the refresh macro

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

4
Run the macro after updates

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.

5
Add new fields if needed

Click the PivotTable and use the PivotTable Fields pane to drag any new fields into Rows, Columns, Values, or Filters.

Create a Dynamic Named Range and Refresh PivotTables with VBA
Keep headers contiguous: COUNTA counts nonempty cells rather than finding the last used column. Blank headers within the source can therefore produce an incorrect range width. The macro refreshes all embedded charts, including ordinary charts; chart sheets are not included.
Free Microsoft Office alternative

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.

Review and edit spreadsheet data used for PivotTable analysis.Open and save Microsoft Excel XLSX files for easier migration.Use a familiar spreadsheet interface for everyday workbook tasks.Work with a free, lightweight office suite.
microsoft office alternative - wps office

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.