In Excel VBA, get the PivotTable associated with the active cell by using ActiveCell.PivotTable; Excel does not provide a single application-wide ActivePivotTable property. The cell must be inside a PivotTable report, so check for a missing object before reading its name, refreshing it, or changing it.
What “active PivotTable” means in Excel
People usually mean the PivotTable that currently has focus when they ask about an “active PivotTable.” In VBA, that usually means the PivotTable containing the active cell on the active worksheet. For example, if the selected cell is a value inside a sales summary, ActiveCell.PivotTable can return the PivotTable object for that summary.
This is different from the active worksheet, the current selection, or an Excel data table. A worksheet can contain several PivotTables, and a workbook can contain many more. Excel does not infer which one you mean merely because a workbook is open. The active cell supplies the context. If it is outside a PivotTable, there may be no PivotTable object to return.
That distinction matters for automation. A macro that works when a user has clicked inside a report may fail when run from a button on another sheet, from the Visual Basic Editor, or after a user selects a chart or slicer. For repeatable tasks, explicitly refer to a worksheet and PivotTable by name instead of relying on the current selection.
How the VBA object model identifies it
Excel’s object model represents a PivotTable as a PivotTable object. A worksheet’s PivotTables collection contains its PivotTables, while a range’s PivotTable property identifies the report associated with that range. The common active-cell pattern is therefore to ask the active cell for its PivotTable, then use the returned object.
The returned object gives code access to useful details, including the PivotTable’s name, its worksheet through its parent, its fields, and methods such as RefreshTable. The cell is just a way to locate the object; once assigned to a variable, the macro can work with the PivotTable directly. This avoids repeatedly asking Excel what is selected.
One subtle point: the Selection object is not necessarily a single cell. A user can select a range, a chart, or another object. ActiveCell is often a more useful starting point for a cell-based lookup, but it still depends on the active sheet being a worksheet and that cell being inside the report.
Get the PivotTable under the active cell
This example displays the name of the PivotTable containing the active cell. It checks the active sheet and handles the case where the cell is not part of a report rather than assuming a PivotTable exists.
Sub ShowActivePivotTable()
Dim ws As Worksheet
Dim pt As PivotTable
' A chart sheet has no worksheet cells to inspect.
If TypeName(ActiveSheet) <> "Worksheet" Then
MsgBox "Activate a worksheet and click inside a PivotTable."
Exit Sub
End If
Set ws = ActiveSheet
' The active cell may not belong to a PivotTable.
On Error Resume Next
Set pt = ActiveCell.PivotTable
On Error GoTo 0
If pt Is Nothing Then
MsgBox "The active cell is not inside a PivotTable."
Exit Sub
End If
MsgBox "PivotTable: " & pt.Name & vbCrLf & _
"Worksheet: " & ws.Name
End Sub
The error handling is deliberately narrow: it surrounds only the attempt to get the PivotTable, then turns normal error handling back on. Depending on the selected range and Excel context, a lookup outside a report may return no object or raise an error. The following Nothing test handles the no-object case. If you adapt the macro, avoid leaving On Error Resume Next active for the rest of a procedure, because it can hide unrelated problems.
Refer to a PivotTable by name instead
If a macro should always work on a particular report, address it through its worksheet’s PivotTables collection. This is generally more dependable than requiring the user to select the right cell first. Replace the sample sheet and PivotTable names with the names in your workbook.
Sub ShowNamedPivotTable()
Dim ws As Worksheet
Dim pt As PivotTable
' Use the workbook that contains this VBA code.
Set ws = ThisWorkbook.Worksheets("Summary")
Set pt = ws.PivotTables("PivotTable1")
MsgBox "PivotTable: " & pt.Name & vbCrLf & _
"Worksheet: " & ws.Name
End Sub
ThisWorkbook means the workbook containing the macro, not necessarily the workbook currently in front of the user. That is usually a good choice for code stored in the workbook it manages. If your macro is in Personal.xlsb or an add-in, use a reference to the intended workbook instead. PivotTable names can be inspected by selecting a PivotTable and checking the PivotTable Name box on the contextual PivotTable ribbon tab; names are not always the same as worksheet names or source-table names.
Read properties and make changes
Once the variable refers to a valid PivotTable, use it directly. For example, pt.Name returns its name, and pt.Parent.Name identifies its worksheet. Code can also examine the PivotTable’s fields and layout. A field’s position and orientation determine whether it appears in rows, columns, filters, or values, so a macro that changes fields should check the report’s current structure rather than assume it is unchanged.
For instance, a report might be named “SalesByRegion” on the “Summary” sheet. A macro can locate it explicitly, then use its name in a message, log, or later operation. This is useful when a workbook has two reports on the same sheet: the active-cell method identifies whichever report contains the selected cell, while the named method intentionally identifies the one specified in code.
Do not confuse a PivotTable with the source Excel table or source range. The PivotTable is the summarized report; the source contains the underlying records. Changing a field in the report does not necessarily change the source data, and refreshing a report does not correct missing or incorrectly typed source records.
Refresh the active report safely
To refresh the PivotTable associated with the active cell, first obtain the object and confirm it exists, then call RefreshTable. This method refreshes the report from its PivotTable data source. A refresh can change visible values, item lists, and the report’s displayed results, so it is sensible to test the macro on a copy of an important workbook.
Sub RefreshActivePivotTable()
Dim pt As PivotTable
Dim refreshed As Boolean
If TypeName(ActiveSheet) <> "Worksheet" Then
MsgBox "Activate a worksheet and click inside a PivotTable."
Exit Sub
End If
On Error Resume Next
Set pt = ActiveCell.PivotTable
On Error GoTo 0
If pt Is Nothing Then
MsgBox "Select a cell inside the PivotTable to refresh."
Exit Sub
End If
' Refresh the report and report whether the operation succeeded.
refreshed = pt.RefreshTable
If refreshed Then
MsgBox pt.Name & " was refreshed."
Else
MsgBox "Excel could not refresh " & pt.Name & "."
End If
End Sub
Refreshing one report is not the same as changing the source data. If new records are outside a fixed source range, the PivotTable may not include them until its source is expanded or converted to an Excel table. PivotTables can also share a PivotCache. Refreshing cache data can affect other reports that use that cache, so consider the workbook’s design when choosing between refreshing a report and refreshing its cache.
Common problems and practical checks
- The active cell is outside the report. Click a cell in the PivotTable body, row labels, or other report area, or use a named reference instead.
- The active sheet is not a worksheet. A chart sheet has no cell-based PivotTable context. Activate the worksheet that contains the report.
- The name is wrong. A misspelled worksheet or PivotTable name causes an error when using the named approach. Check the actual names in Excel and ensure the code points to the correct workbook.
- The report does not show new records. Confirm that the PivotTable source includes those records, then refresh. A refresh cannot add records that are outside a fixed source range.
- The macro runs from a button or event. The active cell might not be the report the user expects. A button on another sheet or a slicer interaction can leave a different cell active; use an explicit worksheet and PivotTable name for predictable behavior.
- Errors are being hidden. Keep error suppression limited to the object lookup, then restore normal handling as shown. Add more specific error handling if a workbook may lack a required sheet or named PivotTable.
Excel versions, Mac, and the web
The VBA examples use standard Excel object-model features and are intended for desktop Excel for Microsoft 365 on Windows. Excel for Mac also supports VBA and PivotTables, although keyboard shortcuts and some platform-specific VBA capabilities can differ. On Windows, Alt+F11 opens the Visual Basic Editor. On many Mac keyboards, Option+F11 opens it; a function-key setting may mean you also need to press Fn.
Excel for the web does not run VBA macros or provide the desktop Visual Basic Editor. A workbook containing VBA can be opened in the web app, but run the macro in desktop Excel. If you need browser-based automation, Office Scripts are a separate option with a different language and object model; they are not VBA and do not use the same code shown here.
Save and run macros responsibly
Save a workbook that contains VBA as an Excel Macro-Enabled Workbook (.xlsm) so the code can be retained. Before running a macro that refreshes or changes a report, save a copy of the workbook. VBA actions generally cannot be undone with Ctrl+Z, so a backup is the practical way to recover from an unintended change.
Only enable macros in files you trust. On Windows, macro security options are in File > Options > Trust Center > Trust Center Settings > Macro Settings. Avoid lowering security globally just to run one file; follow your organization’s policy and use a trusted source. The examples above only inspect or refresh a PivotTable, but other macros can modify data or workbook contents.
Frequently Asked Questions
Does Excel VBA have an ActivePivotTable property?
No single global ActivePivotTable property is the usual way to identify the current report. Use ActiveCell.PivotTable when the active cell is inside a PivotTable, or address a report through a worksheet’s PivotTables collection.
How do I find the active PivotTable’s name?
Assign ActiveCell.PivotTable to a PivotTable variable after checking that the active cell belongs to a report, then read pt.Name. The first code example displays both the report name and worksheet name.
Why does ActiveCell.PivotTable return an error or Nothing?
The active cell may be outside a PivotTable, the active sheet may not be a worksheet, or the current Excel context may not resolve to a report. Check the sheet type and handle a missing object before using its properties.
Can I refresh a PivotTable without selecting it?
Yes. Get it directly with a reference such as ThisWorkbook.Worksheets("Summary").PivotTables("PivotTable1"), then call RefreshTable. This avoids depending on the active cell.
Can I run PivotTable VBA in Excel for the web?
No. The web app does not execute VBA. Open the workbook in desktop Excel for Windows or Mac to run the macro; Office Scripts are a separate web-automation option.
Related
- Excel Named Ranges List: Find, View, and Manage Defined Names
- Excel Named Range Shortcut: Create, Edit, and Use Names
- Excel Page Break Dotted Lines: How to Move Them in Print
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA: Create a New Workbook with Workbooks.AddTo create a new workbook in Excel VBA, use Set wbNew = Workbooks.Add. This opens a workbook in…
- VBA MsgBox Location: Where It Appears and How to Control ItA VBA MsgBox has no built-in argument for setting its screen coordinates. In Excel for Microsoft 365 on…
- Excel VBA Copy and Paste Cells: Examples and OptionsTo copy a cell with Excel VBA, use the source range’s Copy method and specify a destination: sourceCell.Copy…
- Excel VBA: Get the Last Day of a MonthIn Excel VBA, use DateSerial(Year(d), Month(d) + 1, 0) to get the final date of the month containing…
- How to Find a VBA Project Password in Excel SafelyExcel cannot display or reveal a forgotten VBA project password. To regain legitimate access, check your password manager…
- Excel VBA: Find a Value in a Range and Select Its CellIn Excel VBA, use Range.Find to locate a value, check whether it returned a cell, and then activate…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.