Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA - Filter Pivot Based on Cell Reference - Check For No Values

Posted on 2010-09-10
5
Medium Priority
?
1,490 Views
Last Modified: 2012-05-10
I have the code below that auto filters a pivot table, "pvtTwo", based on a cell reference.  The cell references another pivot table, ("pvtOne").  So basically selecting a value to filter by in pivot table 1 auto filters for the same value in pivot table 2.
I'm noticing that when the value to filter by in pivot table 1 is not available in the data source of pivot table 2, it writes over the previously selected value in pivot table 2 that was showing.  Is there any way I can amend the code below to not re-label the value in Pivot 2?
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    On Error Resume Next
    Application.EnableEvents = False
    Me.PivotTables("pvtTwo").PivotFields ("State").CurrentPage = Range("AI7").Value
    Application.EnableEvents = True
End Sub

Open in new window

0
Comment
Question by:error_prone
[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
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:alainbryden
ID: 33649785
Perhaps you could upload a sample file that demonstrates your problem.
0
 

Author Comment

by:error_prone
ID: 33649864
Here's a sample.  Notice that when you select "Orange" from the pivot on the left, (One), then the other pivot correctly also filters to Orange, (with 120 units).  But if you select "Banana" from pivot One, it renames Orange in pivot Two to Banana, (because Banana does not exist in Two).  I want it to show an empty pivot if the value is not available, (or something like that).  But it should not interfere with the ability to filter in Pivot One.
TEST.xlsm
0
 
LVL 39

Accepted Solution

by:
nutsch earned 2000 total points
ID: 33650151
Try this update to your code, to validate existence before update.

Thomas

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    On Error Resume Next
    Application.EnableEvents = False
    Dim pt As PivotItem
    For Each pt In Me.PivotTables("pvtTwo").PivotFields("Fruit").PivotItems
        If pt.Name = Range("B8").Value Then
            Me.PivotTables("pvtTwo").PivotFields("Fruit").CurrentPage = Range("B8").Value
            GoTo endline:
        End If
    Next pt
    MsgBox "No matching data"
endline:
    Application.EnableEvents = True
End Sub

Open in new window

0
 

Author Closing Comment

by:error_prone
ID: 33650176
Perfect, thank you!!
0
 
LVL 39

Expert Comment

by:nutsch
ID: 33650251
Glad to help, thanks for the grade.

Thomas
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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 demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

715 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