Macro for a Pivot Table selection

nilsan7

New Member
Joined
May 22, 2011
Messages
10
Hi there, I have a simple question. On a worksheet, I have 2 Pivot Tables looking at the same data. I would like the user to select an item in Table1 and the Table2 should also use that same option. How do I write a macro for that? So what I mean is, let us say the user has to select a city and there are 10 cities, s/he chooses AUCKLAND in the PivotTable1 under CITY and the same should be selected automatically in PivotTable2 under CITY. I have 2 charts linked to these 2 Tables which will update automatically so all I want is the user to select it once. Can you please help me? Thanks
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
What's the purpose of having 2 separate pivot tables if one should show what the other should show? Can you not have 2 charts off just one table?
 
Upvote 0
Hello, the data has 2 sets of numbers that I want to extract and hence I have 2 Pivots. For eg: AUCKLAND may have the population listed in numbers but daily cash sales in Dollars and they are 2 data sets and hence I have 2 tables, one for numbers and one for $$. This example by the way is only an example. Hope this clarifies, thank you.
 
Upvote 0
Let me put it this way, I just want a macro which will run the following code when there is a change in selection within another PivotTable1.

Range("F5").Select
ActiveSheet.PivotTables("PivotTable4").PivotFields("Area").CurrentPage = Range("b4").Text


Please help - thanks
 
Upvote 0
Let me put it this way, I just want a macro which will run the following code when there is a change in selection within another PivotTable1.

Range("F5").Select
ActiveSheet.PivotTables("PivotTable4").PivotFields("Area").CurrentPage = Range("b4").Text


Please help - thanks

Maybe this
(copy to the worksheet code-page)

Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    If Target = PivotTables("PivotTable1") Then
        Application.EnableEvents = False
        Range("F5").Select
        ActiveSheet.PivotTables("PivotTable4").PivotFields("Area").CurrentPage = Range("b4").Text
        Application.EnableEvents = True
    End If
End Sub

HTH

M.
 
Upvote 0
Great - this code works - thanks a lot :)

If I have to learn more VBA codes etc, which is the best site or book, what would you recommend please? Thanks
 
Upvote 0
Great - this code works - thanks a lot :)

If I have to learn more VBA codes etc, which is the best site or book, what would you recommend please? Thanks

You are welcome.

Yes this code works, but if you are trying to do what i'm supposing - to set the same filter in both PageFields in the two PVTs, ie when you select, say Area1, in one PVT page-field the other PVT also gets filtered to show only Area1 - maybe this is a better and more elegant code.

Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    If Target = PivotTables("PivotTable1") Then
        Application.EnableEvents = False
        PivotTables("PivotTable4").PageFields("Areas").CurrentPage = _
         PivotTables("PivotTable1").PageFields("Areas").CurrentPage.Name
        Application.EnableEvents = True
    End If
End Sub

A very good book:
EXCEL 2010 Power Programming with VBA
John Walkenbach

Ths for providing feedback :)

M.
 
Upvote 0
Thanks marcelo - however, the new code does not work? I have changed the Areas to Area so it is not that. Also, not that I am still using Excel 2003 and not 2010. Just so you know, I work in Auckland, NZ. Thanks again for your help and advice on the book. Any website you recommend following as well for tips and views on coding etc?
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,822
Members
452,946
Latest member
JoseDavid

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