Updating Pivotfields from a Data Validation list across multiple Pivot Tables on Multiple sheets

Carpenter447

New Member
Joined
Mar 5, 2012
Messages
10
Hello I have an issue that i have been playing around with my self for quite sometime now. I found a code that should work but for some reason it is not working and I haven't a clue why. Any help would be appreciated.

Goal : Code that will take a drop list and modify the pivot filters on multiple pivot tables on multiple sheets simultaneously. I.e. Drop list selection is Brown; Brown is part of the Filter Colors; All subsequent pivot tables with the same Filter for colors would then switch to brown and update the data within it.

Issue : The coding works on a sample set that I ripped from contexures.com but not for my data as it stands now.

Additional Info : I am running Excel 2007, I import that data within my pivots from an OLAP cube, & even though I work with macros quite often my actual verbatim writing skills are limited.

So the code below is an adapted version of what you can find on contextures website. I want to be able change three unique Pivot Fields simultaneously. The code string is simply triplicated below with the different fields inputted. When I use the initial code from contexture.com it works but then when i change the Pivotfield nothing happens.

Code:
Sub UpdatePivotFields()
Dim ws As Worksheet
Dim pt As PivotTable
Dim pi As PivotItem
Dim strField1 As String
Dim strField2 As String
Dim strField3 As String

On Error Resume Next
Application.ScreenUpdating = False
Application.EnableEvents = False

'This sector changes strField1 or "Top Customer Name US"
strField1 = "Top Customer Name US"
    If Target.Address = Range("D2").Address Then
        For Each ws In ThisWorkbook.Worksheets
            For Each pt In ws.PivotTables
                With pt.PageFields(strField1)
                    For Each pi In .PivotItems
                        If pi.Value = Target.Value Then
                            .CurrentPage = Target.Value
                            Exit For
                        Else
                            .CurrentPage = "(All)"
                        End If
                    Next pi
                End With
            Next pt
        Next ws
    End If

'This sector changes strField2 or "_Plant"
strField2 = "_Plant"
    If Target.Address = Range("M2").Address Then
        For Each ws In ThisWorkbook.Worksheets
            For Each pt In ws.PivotTables
                With pt.PageFields(strField1)
                    For Each pi In .PivotItems
                        If pi.Value = Target.Value Then
                            .CurrentPage = Target.Value
                            Exit For
                        Else
                            .CurrentPage = "(All)"
                        End If
                    Next pi
                End With
            Next pt
        Next ws
    End If

'This sector changes strField3 or "Month"
strField3 = "Month"
    If Target.Address = Range("U2").Address Then
        For Each ws In ThisWorkbook.Worksheets
            For Each pt In ws.PivotTables
                With pt.PageFields(strField1)
                    For Each pi In .PivotItems
                        If pi.Value = Target.Value Then
                            .CurrentPage = Target.Value
                            Exit For
                        Else
                            .CurrentPage = "(All)"
                        End If
                    Next pi
                End With
            Next pt
        Next ws
    End If
End Sub
 
Here is the macro for manually applying the filter for the 1st two tables:
Sub Macro3()
'
' Macro3 Macro
'

'
ActiveSheet.PivotTables("CustYoY").PivotFields( _
"[Ship To Customer].[Customer Enterprise].[Customer Enterprise]"). _
ClearAllFilters
ActiveSheet.PivotTables("CustYoY").PivotFields( _
"[Ship To Customer].[Customer Enterprise].[Customer Enterprise]"). _
CurrentPageName = _
"[Ship To Customer].[Customer Enterprise].&[177_OPEN Chicago]"
Sheets("CustRoll12").Select
ActiveSheet.PivotTables("CustRoll").PivotFields( _
"[Ship To Customer].[Customer Enterprise].[Customer Enterprise]"). _
ClearAllFilters
ActiveSheet.PivotTables("CustRoll").PivotFields( _
"[Ship To Customer].[Customer Enterprise].[Customer Enterprise]"). _
CurrentPageName = _
"[Ship To Customer].[Customer Enterprise].&[177_OPEN Chicago]"
End Sub

Although, as I mentioned above, it seems to be an issue with whichever table is listed third in the pivot tables to update array.
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
That's surprising. That implies that an error occured after PT was set to "CustYoY" but before reaching the Select Case block that assigns sPage and sField.

This is more challenging than most threads because I'm not able to mockup your OLAP scenario and test it. I don't mind at all, but I'm sorry to put you through so much back and forth.

If I were debugging this myself, I'd set a Breakpoint which would stop the code at a certain point, then use the F8 key to step through each line of code to watch what is happening. You could try that however that's a little more advanced than what we've been doing.

As an easier alternative, try commenting out the 3 statements that set the error handler. This will cause the debugger to stop at the statement that throws the error and provide us with an error code to give us more clues.

The 3 instances of error handler statements to comment out are....

Code:
'           On Error GoTo CleanUp

'           On Error Resume Next
          
'           On Error GoTo CleanUp

Make sure to test with a PivotItem selection that exists in each PivotTable, so we don't just catch the designed error of trying to filter by an item that doesn't exist.
 
Upvote 0
Sorry for the jargon. :p

That means putting a " ' " quote mark in front of those statements so that VBA treats those as comments or remarks instead of executable statements.
 
Upvote 0
I hope I did this right....
I changed the error handler statements to:
'On Error GoTo CleanUp
'On Error Resume Next
'On Error GoTo CleanUp

I received an error "Run-time Error '9': Subscript out of Range and it pointed to
Select Case vPTName(i)

I hope this helps....I'm sorry to be taking up so much of your time with this....
 
Upvote 0
That was the clue we needed. I found my mistake - I had referenced the wrong variable in the Select Case ("vPTName" instead of "vPTNames")
vPTNames has 4 elements: the 4 Sheet!Pivot codes
vPTName has 2 elements: the SheetName and PivotName

The code would error when it tried to get the 3rd element in vPTName- which is the pattern that you were observant to discover.

Please correct this part....
Code:
       Select Case vPTNames(i)
            Case "Brand Trend!BrandTrend" 'exception
                Debug.Print vPTNames(i) & ": Exception"
                sField = "[Enterprise].[Enterprise Name].[Enterprise Name]"
                sPage = "[[Ship To Customer].[Customer Enterprise].&[" _
                    & Split(Target, "_")(0) & "]"
                    
            Case Else  'default
                Debug.Print vPTNames(i) & ": Default"
                sField = "[Ship To Customer].[Customer Enterprise]." & _
                    "[Customer Enterprise]"
                    
                    '-- extract the Enterprise Code from the value
                sPage = "[Enterprise].[Enterprise Name].&[" _
                    & Target & "]"
            
        End Select

We might still have to tweak the sPage value, but this should fix the "stop after two pivots" problem.
 
Upvote 0
Would you believe me if I told you I kept looking at that vPTName vs vPTNames and wondering if it should be vPTNames?! :biggrin:

ANYWAY.....YOU ROCK!!!! I got it to work....first time I copied your code it just cleared all filters, so I changed the sPage to match the sField and it works like a charm.

I can't thank you enough for all your help. Going to make life easier for me and the managers I support!!!!

Enjoy the rest of your day!!!!
 
Upvote 0
Great to hear that worked! (y)

It's a good example of how error handling can mask problems making it harder to find them during code development.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,221,475
Messages
6,160,056
Members
451,615
Latest member
soroosh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top