To point a pivot table at a specific range with VBA, set the SourceData property of the pivot cache to a range address string or a named range, then refresh. The shortest working pattern is ActiveSheet.PivotTables(1).PivotCache.SourceData = "Sheet1!A1:D100" followed by .RefreshTable. This lets you rebuild or retarget a pivot without touching the field layout manually.

What You'll Need

  • Excel desktop for Windows or Mac (VBA is not available in Excel for the web).
  • A workbook saved as .xlsm or .xlsb so macros persist.
  • The Developer tab enabled, or access to the Visual Basic Editor via Alt+F11 (Option+F11 on Mac).
  • A source data range with a clear header row and no blank header cells.

Menu and dialog labels can differ slightly by Excel version, platform and region. The steps below use the Windows desktop interface; Mac shortcuts are noted where they differ.

Step-by-Step

  1. Name your source range first. Select the data including headers, then type a name such as SalesData into the Name Box left of the formula bar and press Enter. A named range is more reliable than a hard-coded address because it survives inserted rows and columns.
  2. Open the Visual Basic Editor. Press Alt+F11 (Option+F11 on Mac). In the Project pane, right-click your workbook and choose Insert > Module.
  3. Write a macro that sets the source. Paste a skeleton like this and adjust the names:
    Sub SetPivotSource()
        Dim ws As Worksheet
        Dim pt As PivotTable
        Set ws = ThisWorkbook.Worksheets("Report")
        Set pt = ws.PivotTables(1)
        pt.PivotCache.SourceData = ThisWorkbook.Names("SalesData").RefersToRange.Address(True, True, xlA1, True)
        pt.RefreshTable
    End Sub
    The Address arguments force an absolute, external-style reference (workbook, sheet, cells) that the pivot cache accepts.
  4. Run the macro. Press F5 in the editor, or assign the macro to a button on the sheet via Developer > Insert > Button (Form Control).
  5. Verify the change. Click inside the pivot table and check the range shown in the PivotTable Analyze tab > Change Data Source dialog. It should match your named range.
  6. Handle a new pivot table. If no pivot exists yet, create the cache and table in code instead:
    Dim pc As PivotCache
    Dim pt As PivotTable
    Set pc = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=ThisWorkbook.Names("SalesData").RefersToRange)
    Set pt = pc.CreatePivotTable(TableDestination:=ThisWorkbook.Worksheets("Report").Range("A3"), TableName:="SalesPivot")
    This avoids relying on an existing table's cache.
  7. Refresh on open if the data changes. Add ThisWorkbook.RefreshAll to a Workbook_Open event, or set the pivot cache's refresh-on-open behaviour through the PivotTable Options dialog rather than guessing at a property name.

Common Mistakes

  • Using a relative address. "A1:D100" without a sheet name can resolve against the wrong sheet. Always include the sheet, and ideally the workbook, in the string.
  • Leaving a blank header. A pivot cache rejects a source range with empty header cells; Excel raises an error or silently drops the column.
  • Pointing at the pivot's own output. If the new range overlaps the pivot table's location, Excel refuses the change. Move the pivot first.
  • Forgetting to refresh. Changing SourceData does not redraw the pivot until RefreshTable runs.
  • Hard-coding sheet names. Renaming a sheet breaks string addresses. Named ranges or Worksheet.CodeName are safer.

Tips

  • Use a dynamic named range with OFFSET or a table (ListObject) so the source grows automatically. A ListObject's Range updates as rows are added.
  • Wrap the source-setting code in error handling (On Error GoTo) so a missing name or pivot produces a clear message instead of a generic failure.
  • Keep pivot field layout in a separate macro from the source-range macro; mixing them makes debugging harder.
  • Test on a copy of the workbook before running against live data.

Troubleshooting

If the macro errors, work through the simplest checks first. Confirm the workbook is macro-enabled and macros are not disabled in the Trust Center. Check that the pivot table name and sheet name in the code match reality exactly, including spaces.

If Excel reports that the reference is not valid, print the address string to the Immediate window with Debug.Print and compare it to the Change Data Source dialog. A missing sheet name or a stray space is the usual cause.

If the pivot refreshes but shows old data, confirm the cache is the one you changed: a workbook can hold several pivot caches, and PivotTables(1) may belong to a different cache than expected. Loop through PivotCaches and set the source on each, or target the table by name.

If the file is shared or protected, unprotect the sheet and workbook structure before running the macro, then re-protect afterwards. Do not disable protection permanently on a shared file.

Frequently Asked Questions

Can I set the pivot source to an entire column?

Yes, but it is slow and includes blank rows. A named range or table is preferable.

Does changing SourceData keep my field layout?

Yes, the pivot layout is retained as long as the field names in the new range match the old ones.

Why does my macro fail on a Mac?

Shortcuts differ (Option+F11) and some file-path handling differs, but the PivotCache object model is the same. Check that the workbook is .xlsm.

Can I point a pivot at a range on another sheet?

Yes. Include the sheet name in the address string, for example "Data!A1:D200", or use a workbook-level named range.

Will this work in Excel for the web?

No. VBA is not supported in the browser version; use the desktop app.

How do I avoid breaking the pivot when rows are added?

Base the source on a table or a dynamic named range so the reference expands automatically.