Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel 2007 Change Pivot Filter Based On 2 Dynamic Variables

Posted on 2011-03-14
5
Medium Priority
?
258 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 2000 total points
ID: 35132879
Press f5
0
 
LVL 1

Author Closing Comment

by:Escanaba
ID: 35180679
Thank you!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

972 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