How to Reference ONE cell from ALL Pivot Tables in a Workbook - VBA

actjfc

Active Member
Joined
Jun 28, 2003
Messages
416
Excel friends,

The code wks.Range(rngAddress).Activate is not working. It generates a runtime error 91. Can somebody help me. Thanks!

Code:
Sub PivotTableMacro()
Dim wks As Worksheet
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim pt As PivotTable
Dim rngAddress As Range

For Each wks In wb.Sheets
For Each pt In wks.PivotTables
         
               rngAddress = Chr(34) & Left(pt.TableRange1.Address, InStr(1, pt.TableRange1.Address, ":") - 1) & Chr(34)
                 
               wks.Range(rngAddress).Activate 'I need this code to activate or select the cell rngAddress 
                 
               Application.Run "SpecialMacro" ' this Special Macro macro will run only if one cell is selected inside the current pt.

               MsgBox rngAddress ' for example, it shows "G10"

Next pt
Next wks
 
End Sub

Thanks again!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this

Code:
Sub PivotTableMacro()
  Dim wks As Worksheet, wb As Workbook, pt As PivotTable, rngAddress As Range
  Set wb = ActiveWorkbook
  For Each wks In wb.Sheets
    wks.Select
    For Each pt In wks.PivotTables
      Set rngAddress = pt.TableRange1 '.Address
      rngAddress.Cells(1, 1).Select
      MsgBox rngAddress.Cells(1, 1).Address ' for example, it shows "G10"
      'Application.Run "SpecialMacro" ' this Special Macro macro will run only if one cell is selected inside the current pt.
    Next pt
  Next wks
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top