Solved

Excel 2007 Change Pivot Filter Based On 2 Dynamic Variables

Posted on 2011-03-14
5
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
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 Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

624 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