Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: How to Select a Pivot Table

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To select a pivot table with VBA, reference it by its range or by its name. The shortest working method is ActiveSheet.PivotTables("PivotTable1").TableRange1.Select, which selects the entire pivot table (excluding page fields). If you want the whole pivot table including page fields, use TableRange2.Select. If you want to select the pivot table by its location instead of its name, use ActiveSheet.PivotTables(1).TableRange1.Select for the first pivot table on the sheet. This guide shows several reliable ways to select a pivot table, explains the difference between TableRange1 and TableRange2, and covers what to do when the macro fails.

What You'll Need

  • Excel with macros enabled (the .xlsm file format).
  • A worksheet that already contains at least one pivot table.
  • Basic familiarity with the VBA editor (Alt+F11 on Windows, Fn+Option+F11 on Mac).
  • The exact name of the pivot table, which you can find by clicking any cell in it and looking at the PivotTable Name box on the PivotTable Analyze tab.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac). In the Project Explorer, find your workbook and insert a new module via Insert > Module.
  2. Identify the pivot table name. Click any cell inside the pivot table. On the PivotTable Analyze tab, the leftmost box shows the name (for example, PivotTable1). Note that names can differ slightly by Excel version, but the location is consistent.
  3. Write the basic selection line. In the module, type:
    Sub SelectPivot()
    ActiveSheet.PivotTables("PivotTable1").TableRange1.Select
    End Sub
    Replace "PivotTable1" with your actual name.
  4. Run the macro. Press F5 or use Run > Run Sub. The pivot table's data area, row labels, column labels, and grand totals will be selected. Page fields are not included with TableRange1.
  5. Select the entire pivot table including page fields. Change TableRange1 to TableRange2. This selects the whole pivot table including any report filter (page field) area above it.
  6. Select by index instead of name. If you don't know the name, use ActiveSheet.PivotTables(1).TableRange1.Select for the first pivot table on the active sheet. Use 2 for the second, and so on.
  7. Select a specific part (optional). To select only the data body range, use .DataBodyRange.Select. To select only the row labels, use .RowRange.Select. To select only the column labels, use .ColumnRange.Select. These are useful when you need to format or copy a portion.
  8. Handle errors gracefully. Wrap the selection in error handling to avoid a runtime error if the pivot table name is wrong or the sheet is not active:
    On Error Resume Next
    ActiveSheet.PivotTables("PivotTable1").TableRange1.Select
    If Err.Number <> 0 Then MsgBox "Pivot table not found."
    On Error GoTo 0
  9. Save the workbook as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. If you save as .xlsx, the macro will be lost.

Common Mistakes

  • Using the wrong name. Pivot table names are case-sensitive in VBA? Actually they are not case-sensitive, but they must match exactly including spaces. A name like "PivotTable1" is not the same as "Pivot Table 1".
  • Assuming the active sheet is the right one. The macro uses ActiveSheet, so if the user is on a different sheet, the code fails. Use Worksheets("Sheet1") instead of ActiveSheet to be explicit.
  • Confusing TableRange1 and TableRange2. TableRange1 excludes page fields; TableRange2 includes them. Selecting the wrong one may leave out the filter area.
  • Forgetting to handle errors. If the pivot table doesn't exist, the macro stops with a runtime error 1004. Add error handling as shown above.
  • Trying to select a pivot table on a protected sheet. Selection may fail if the sheet is protected. Unprotect the sheet first or use UserInterfaceOnly:=True when protecting.
  • Using Select unnecessarily. In most cases you don't need to select an object to act on it. For example, to format the data body range, use .DataBodyRange.Interior.Color = vbYellow directly. Selecting slows down the macro and can cause issues.

Tips

  • Use Dim pt As PivotTable and Set pt = ActiveSheet.PivotTables("PivotTable1") to work with the pivot table more cleanly and avoid repeated lookups.
  • To loop through all pivot tables on a sheet, use For Each pt In ActiveSheet.PivotTables and then pt.TableRange1.Select.
  • If you need to select a pivot table on a different sheet, activate that sheet first or use Worksheets("Sheet2").PivotTables(1).TableRange1.Select (this works without activating the sheet in most cases).
  • Use Application.ScreenUpdating = False before selecting and True after to speed up macros that manipulate multiple pivot tables.
  • Remember that selecting a pivot table does not change the active cell; the selection is a range object. If you need the active cell inside the pivot table, use pt.TableRange1.Cells(1,1).Activate.

Troubleshooting

If the macro does not select the pivot table, check these in order:

  • Runtime error 1004: The pivot table name is wrong or the sheet is not active. Verify the name and use an explicit worksheet reference.
  • Nothing happens: The macro may have run but the selection is not visible because the sheet is not active. Add ActiveSheet.Activate before selecting, or check that the correct sheet is active.
  • Compile error: Check for typos in the pivot table name or missing quotation marks. Also ensure the module is not missing a reference.
  • Selection includes extra cells: TableRange1 may include blank cells if the pivot table has a large range. Use pt.TableRange1.Select only after confirming the pivot table layout.
  • Protected sheet: Unprotect the sheet via Review > Unprotect Sheet, or modify the macro to unprotect and reprotect.
  • Macro security: If macros are disabled, enable them via File > Options > Trust Center > Trust Center Settings > Macro Settings. Choose "Disable all macros with notification" and click Enable Content when prompted.

Frequently Asked Questions

How do I find the name of a pivot table?

Click any cell in the pivot table. On the PivotTable Analyze tab, the PivotTable Name box shows the name. You can also use ActiveSheet.PivotTables(1).Name in the Immediate Window (Ctrl+G) to list names.

Can I select a pivot table without knowing its name?

Yes. Use the index number: ActiveSheet.PivotTables(1).TableRange1.Select selects the first pivot table on the sheet. Use 2 for the second, and so on.

What is the difference between TableRange1 and TableRange2?

TableRange1 includes the data area, row and column labels, and grand totals but excludes page fields (report filters). TableRange2 includes everything, including the page fields area above the pivot table.

Why does my macro select the wrong pivot table?

If you use ActiveSheet.PivotTables(1), the index refers to the order of pivot tables on that sheet, which may not match the visual order. Use the name instead for reliability.

Can I select a pivot table on a different worksheet?

Yes, but the worksheet must be active for Select to work visibly. Alternatively, use Worksheets("Sheet2").PivotTables(1).TableRange1.Select after activating Sheet2.

How do I select only the data body of a pivot table?

Use ActiveSheet.PivotTables("PivotTable1").DataBodyRange.Select. This selects only the values area, excluding row and column labels.