Solved

Excel 2007 Change Pivot Filter Based On 2 Dynamic Variables

Posted on 2011-03-14
5
246 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now