Solved

Excel 2007 Change Pivot Filter Based On 2 Dynamic Variables

Posted on 2011-03-14
5
248 Views
Last Modified: 2012-05-11
Hello,

Please see attached sample.  Im hoping someone can provide a VB code that when ran will change the filters on the pivot table in sheet1 to what is reflected in cells B10 and E10 on the calculator tab.  The macro should only change this pivot table so if there are others on the calculator tab they will not be affected.

Thanks!
EE-Sample.xlsx
0
Comment
Question by:Escanaba
  • 3
  • 2
5 Comments
 
LVL 39

Expert Comment

by:nutsch
ID: 35129396
Here is code to put in the calculator sheet worksheet module, after naming the office and location dropdowns clOffice and clLocation

Private Sub Worksheet_Change(ByVal Target As Range)
Dim pi As PivotItem

If Not Intersect(Target, Range("clLocation")) Is Nothing Then
    With Sheets("Sheet1").PivotTables("PivotTable1").PivotFields("Location")
        .ClearAllFilters
        .CurrentPage = Range("clLocation").Value
    End With
End If

If Not Intersect(Target, Range("clOffice")) Is Nothing Then
    With Sheets("Sheet1").PivotTables("PivotTable1").PivotFields("Department")
        For Each pi In .PivotItems
            If pi.Name = Range("clOffice") Then pi.Visible = True
        Next
        For Each pi In .PivotItems
            If pi.Name <> Range("clOffice") Then pi.Visible = False
        Next
    End With
End If

End Sub

Open in new window


See attached file for operation

Thomas
filterpivot.xlsm
0
 
LVL 1

Author Comment

by:Escanaba
ID: 35132157
Its generating a 'method range of object worksheet failed error'.
0
 
LVL 1

Author Comment

by:Escanaba
ID: 35132208
Ok...just caught the comment about naming the locations.  Its now generating a different error as follows:

Cant execute code in break mode

at this point:  If Not Intersect(Target, Range("clLocation")) Is Nothing Then
0
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
ID: 35132879
Press f5
0
 
LVL 1

Author Closing Comment

by:Escanaba
ID: 35180679
Thank you!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question