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
 
Ok- the fog is lifting. :)

I misinterpreted what you meant by "the last pivot where the field name differs?" in post #31 and I've been thinking "CustBrand" was the exception (despite your good efforts to correct that).

The code below should correct for that. It also attempts to parse the Enterprise Code from the DV value selected by reading the part of the value before the "_" underscore. Is that a consistent pattern for this field for the BrandTrend pivot?

Code:
'---check which field name goes this Pivot
Select Case vPTName(i)
    Case "Brand Trend!BrandTrend" 'exception
        Debug.Print vPTName(i) & ": Exception"
        sField = "[Enterprise].[Enterprise Name].[Enterprise Name]"
        sPage = "[[Ship To Customer].[Customer Enterprise].&[" _
            & Target & "]"

    
    Case Else  'default
        Debug.Print vPTName(i) & ": Default"
        sField = "[Ship To Customer].[Customer Enterprise]." & _
            "[Customer Enterprise]"

            
            '-- extract the Enterprise Code from the value
        sPage = "[Enterprise].[Enterprise Name].&[" _
            & Split(Target, "_")(0) & "]"
End Select
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
lol :) I'm just thankful for your help.
I tried the above and it doesn't work either.
I'm wondering if we need to have the "exception" as it's own array/pivot filter in the coding....I'm going to play around, but if you come up with anything else, let me know....
THANK-YOU for taking your time to help me figure this out!!!! Really do appreciate it.
 
Upvote 0
Just some information that might help....when I test the file with the original code, all 3 pivot tables that have Ship To Customer. Customer Enterpirse as the filter work perfectly, but for some reason, the new code only applies to the filter to 2 of the 3 tables, even though the 3 tables all use the same Ship To Customer. Customer Enterprise as the filter. That is why I'm wondering if we have to split the code out somehow to look at these 3 tables, and apply the filter, then look at the 4th [exception] table and apply the filter, instead of listing all 4 tables in the same PT array.....I've been playing around with this with not much luck - as I'm very new to VBA coding.....
 
Upvote 0
No worries- it's a good puzzle. I'm confident we can solve this by tracking the execution.

I think the reason that only two Pivots are getting set instead of three, is that the code is throwing an error while attempting to set BrandTrend.
Since this is the 3rd Pivot in the array, execution goes to the CleanUp: error handler and never tries to set the 4th Pivot in the array.

Just to confirm that, I've switched the order of the Pivots in the array so we can isolate the problem to BrandTrend if the first 3 Pivots work correctly.

I've added some additional Debug.Print statements which should give us the information needed to solve the BrandTrend mismatch.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
     '---When the DV Dropdown is changed, sets CurrentPageName
     ' of corresponding PivotField for all listed PivotTables.
     Dim sField As String, sPage As String, i As Long
     Dim PT As PivotTable
     Dim vPTNames As Variant, vPTName As Variant
    
     Const sDV_Addr1 As String = "$B$11"  'Enterprise DV Cell
    
     If Target.Address <> sDV_Addr1 Then Exit Sub
    
     '---Make list of PTs to be Filtered
     vPTNames = Array("Cust YoY!CustYoY", "CustRoll12!CustRoll", _
        "Cust-Brand Trend!CustBrand", "Brand Trend!BrandTrend")
        
     '---Set Parameters based on each PivotTable
     
     On Error GoTo CleanUp
     Application.EnableEvents = False
    
    '---Filter each PT in list using Parameters
    For i = LBound(vPTNames) To UBound(vPTNames)
       vPTName = Split(vPTNames(i), "!")
       Set PT = Sheets(vPTName(0)).PivotTables(vPTName(1))
       
    '---check which field name goes this Pivot
        Select Case vPTName(i)
            Case "Brand Trend!BrandTrend" 'exception
                Debug.Print vPTName(i) & ": Exception"
                sField = "[Enterprise].[Enterprise Name].[Enterprise Name]"
                sPage = "[[Ship To Customer].[Customer Enterprise].&[" _
                    & Target & "]"
            
            Case Else  'default
                Debug.Print vPTName(i) & ": Default"
                sField = "[Ship To Customer].[Customer Enterprise]." & _
                    "[Customer Enterprise]"
                    
                    '-- extract the Enterprise Code from the value
                sPage = "[Enterprise].[Enterprise Name].&[" _
                    & Split(Target, "_")(0) & "]"
        End Select
        Debug.Print vbCr & "Attempting Pivot: " & PT.Name & vbCr _
            & "  sField: " & sField & vbCr & "  sPage: " & sPage
        
        With PT.PivotFields(sField)
           .ClearAllFilters
           On Error Resume Next
           .CurrentPageName = sPage
           On Error GoTo CleanUp
           
        End With
     Next i
    
CleanUp:
    If Err.Number <> 0 Then _
        Debug.Print vbCr & "Threw error while processing: & PT.Name & vbCr" _
            & "  sField: " & sField & vbCr & "  sPage: " & sPage
     Application.EnableEvents = True
End Sub

Give this a try and post the debug info from BrandTrend.
 
Upvote 0
I tried that yesterday as well (rearranging the tables), and tried again with your revised code, but that 3rd table Cust-Brand Trend (the 3rd table with the same filter) is still not updating.

Here is the error code:
[TABLE="width: 781"]
<TBODY>[TR]
[TD]Cust YoY: Default[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Attempting Pivot: CustYoY[/TD]
[/TR]
[TR]
[TD] sField: [Ship To Customer].[Customer Enterprise].[Customer Enterprise][/TD]
[/TR]
[TR]
[TD] sPage: [Enterprise].[Enterprise Name].&[177][/TD]
[/TR]
[TR]
[TD]CustRoll: Default[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Attempting Pivot: CustRoll[/TD]
[/TR]
[TR]
[TD] sField: [Ship To Customer].[Customer Enterprise].[Customer Enterprise][/TD]
[/TR]
[TR]
[TD] sPage: [Enterprise].[Enterprise Name].&[177][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Threw error while processing: & PT.Name & vbCr sField: [Ship To Customer].[Customer Enterprise].[Customer Enterprise][/TD]
[/TR]
[TR]
[TD] sPage: [Enterprise].[Enterprise Name].&[177][/TD]
[/TR]
</TBODY><COLGROUP><COL></COLGROUP>[/TABLE]
 
Upvote 0
I tried applying the manual filter again, to the Cust-Brand Trend sheet, since this is where is seems to get hung up. Here is the macro for applying the filter manually. (not sure if this will help you or not)

' Macro1 Macro
'
'
ActiveSheet.PivotTables("CustBrand").PivotFields( _
"[Ship To Customer].[Customer Enterprise].[Customer Enterprise]"). _
ClearAllFilters
ActiveSheet.PivotTables("CustBrand").PivotFields( _
"[Ship To Customer].[Customer Enterprise].[Customer Enterprise]"). _
CurrentPageName = _
"[Ship To Customer].[Customer Enterprise].&[177_OPEN Chicago]"
End Sub
 
Upvote 0
Oops. That output shows I was parsing the Enterprise Code on the Default instead of the Exception. Try this corrected version...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
     '---When the DV Dropdown is changed, sets CurrentPageName
     ' of corresponding PivotField for all listed PivotTables.
     Dim sField As String, sPage As String, i As Long
     Dim PT As PivotTable
     Dim vPTNames As Variant, vPTName As Variant
    
     Const sDV_Addr1 As String = "$B$11"  'Enterprise DV Cell
    
     If Target.Address <> sDV_Addr1 Then Exit Sub
    
     '---Make list of PTs to be Filtered
     vPTNames = Array("Cust YoY!CustYoY", "CustRoll12!CustRoll", _
        "Cust-Brand Trend!CustBrand", "Brand Trend!BrandTrend")
        
     '---Set Parameters based on each PivotTable
     
     On Error GoTo CleanUp
     Application.EnableEvents = False
    
    '---Filter each PT in list using Parameters
    For i = LBound(vPTNames) To UBound(vPTNames)
       vPTName = Split(vPTNames(i), "!")
       Set PT = Sheets(vPTName(0)).PivotTables(vPTName(1))
       
    '---check which field name goes this Pivot
        Select Case vPTName(i)
            Case "Brand Trend!BrandTrend" 'exception
                Debug.Print vPTName(i) & ": Exception"
                sField = "[Enterprise].[Enterprise Name].[Enterprise Name]"
                       '-- extract the Enterprise Code from the value
                sPage = "[[Ship To Customer].[Customer Enterprise].&[" _
                    & Split(Target, "_")(0) & "]"
                    
            Case Else  'default
                Debug.Print vPTName(i) & ": Default"
                sField = "[Ship To Customer].[Customer Enterprise]." & _
                    "[Customer Enterprise]"
                    
                sPage = "[Enterprise].[Enterprise Name].&[" _
                    & Target & "]"
            
        End Select
        Debug.Print vbCr & "Attempting Pivot: " & PT.Name & vbCr _
            & "  sField: " & sField & vbCr & "  sPage: " & sPage
        
        With PT.PivotFields(sField)
           .ClearAllFilters
           On Error Resume Next
           .CurrentPageName = sPage
           On Error GoTo CleanUp
           
        End With
     Next i
    
CleanUp:
    If Err.Number <> 0 Then _
        Debug.Print vbCr & "Threw error while processing: " & PT.Name & vbCr _
            & "  sField: " & sField & vbCr & "  sPage: " & sPage
     Application.EnableEvents = True
End Sub
 
Upvote 0
Oh no....I tried that but it cleared all the filters, so now even the ones that were working have no filter applied. So I changed the sPage to match the sField (i.e. for the exception I changed sPage to [Enterprise].[Enterprise Name] and for the default I changed sPage to [Ship To Customer].[Customer Enterprise], but it still gets hung up on the CustBrand pivot :(

Here is the error log:
[TABLE="width: 448"]
<TBODY>[TR]
[TD]Cust YoY: Default[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Attempting Pivot: CustYoY[/TD]
[/TR]
[TR]
[TD] sField: [Ship To Customer].[Customer Enterprise].[Customer Enterprise][/TD]
[/TR]
[TR]
[TD] sPage: [Ship To Customer].[Customer Enterprise].&[177_OPEN Chicago][/TD]
[/TR]
[TR]
[TD]CustRoll: Default[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Attempting Pivot: CustRoll[/TD]
[/TR]
[TR]
[TD] sField: [Ship To Customer].[Customer Enterprise].[Customer Enterprise][/TD]
[/TR]
[TR]
[TD] sPage: [Ship To Customer].[Customer Enterprise].&[177_OPEN Chicago][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Threw error while processing: CustBrand[/TD]
[/TR]
[TR]
[TD] sField: [Ship To Customer].[Customer Enterprise].[Customer Enterprise][/TD]
[/TR]
[TR]
[TD] sPage: [Ship To Customer].[Customer Enterprise].&[177_OPEN Chicago][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Many thanks!!!!![/TD]
[/TR]
</TBODY><COLGROUP><COL></COLGROUP>[/TABLE]
 
Upvote 0
Hmmm...try going back and recording macros for each Pivot while manually setting the filters (like you did for Macro5 and Macro6).

It sounds like your first two pivots might need to be filtered by the parsed Enterprise Code instead of the full display name.

I don't understand why that would have changed from your earliest successful attempts using the full display name, but if those first two were working with the code in post #44 where I had accidently parsed the Default, that implies they need the parsed Enterprise Code.
 
Upvote 0
This is what I find interesting....it seems that the 3rd table listed in the array is always the one that get's hung up. To test this, I switched Cust-Brand Trend!CustBrand and CustYoY!CustYoY (so Cust-Brand Trend is now first and CustYoY is third), and sure enough, Cust-Brand Trend got updated but CustYoY did not

'---Make list of PTs to be Filtered
vPTNames = Array("Cust-Brand Trend!CustBrand", "CustRoll12!CustRoll", _
"Cust YoY!CustYoY", "Brand Trend!BrandTrend")


Here is the error log:
Cust-Brand Trend: Default
Attempting Pivot: CustBrand
sField: [Ship To Customer].[Customer Enterprise].[Customer Enterprise]
sPage: [Ship To Customer].[Customer Enterprise].&[177_OPEN Chicago]
CustRoll: Default
Attempting Pivot: CustRoll
sField: [Ship To Customer].[Customer Enterprise].[Customer Enterprise]
sPage: [Ship To Customer].[Customer Enterprise].&[177_OPEN Chicago]
Threw error while processing: CustYoY
sField: [Ship To Customer].[Customer Enterprise].[Customer Enterprise]
sPage: [Ship To Customer].[Customer Enterprise].&[177_OPEN Chicago]
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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