VBA to Change Filter in Multiple Pivot Tables based on A Cell Value

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.พ. 2025
  • This Excel VBA Tutorial explains VBA Code to Apply Filter in Multiple Pivot Tables. We can use drop down list from a Cell as Filter for multiple pivot tables. VBA Code will loop through all pivot tables on the sheet and change value in filter as per selected value.
    Website :- WWW.EXCELDESTINATION.COM

ความคิดเห็น • 97

  • @Henry4YWH
    @Henry4YWH ปีที่แล้ว +2

    Out of all the sites I checked, this is the only one that worked. Thanks!

  • @sirajkhan6945
    @sirajkhan6945 2 ปีที่แล้ว +1

    Works for me, thank you so much, with loads of love.

  • @CesarLlanosBaron
    @CesarLlanosBaron 4 ปีที่แล้ว +1

    Awesome!!! thank you very much, Im going to try it right now... I have 14 Pivot Tables in one Sheet!!!!

  • @sarano8405
    @sarano8405 2 ปีที่แล้ว

    Thanks so much, great video. Is there a way to set all Pivot tables to refresh immediately (without having to click that Refresh button) when a cell value at dropdown list changes?

  • @joramandrew3805
    @joramandrew3805 ปีที่แล้ว

    This is great and very helpful however does this code work for pivot tables connected using data model.?

  • @adrianobrito8007
    @adrianobrito8007 4 ปีที่แล้ว +2

    Hi! tks for the explanation.. how can I change the code to include 2 more cells with dropdown for the filter?

    • @ExcelDestination
      @ExcelDestination  4 ปีที่แล้ว

      that would require some additional code

    • @adolfmontaner9937
      @adolfmontaner9937 4 ปีที่แล้ว +1

      @@ExcelDestination You can try this one I also added two for dropdown on mine and here's the code.
      Sub Btn_Filter()
      Dim a As String
      Dim b As String
      Dim c As String
      Dim pt As PivotTable
      Dim pt2 As PivotTable
      Dim pt3 As PivotTable
      ThisWorkbook.Worksheets("Ledger").Activate
      a = Worksheets("Ledger").Cells(3, 1).Value
      For Each pt In ActiveSheet.PivotTables
      With pt.PivotFields("WBS ID")
      .ClearAllFilters
      .CurrentPage = a

      End With
      Next
      b = Worksheets("Ledger").Cells(3, 2).Value
      For Each pt2 In ActiveSheet.PivotTables
      With pt2.PivotFields("Accounting year")
      .ClearAllFilters
      .CurrentPage = b

      End With
      Next
      c = Worksheets("Ledger").Cells(3, 3).Value
      For Each pt3 In ActiveSheet.PivotTables
      With pt3.PivotFields("Accounting month")
      .ClearAllFilters
      .CurrentPage = c

      End With
      Next
      End Sub

  • @emilytran84
    @emilytran84 5 หลายเดือนก่อน

    How this work with multiple value instead of one cell ? Thank you.

  • @kissxk
    @kissxk 3 ปีที่แล้ว +1

    Hi, thanks for this. is the command any different for a powerpivot table where the source is the datamodel? thanks

  • @robhermany
    @robhermany 3 ปีที่แล้ว +1

    You have great videos. Thank you for all of your help! Similar to your form video where you update the source data with a "Search and Display button", how would you update multiple pivot tables and slicers/upon entering in an opp_ID into a textbox form? In other words, instead of changing a filter in multiple pivot tables based on a cell value, it would be to change filter in multiple pivot tables based on a VBA form input / text box?

  • @indianmonster1445
    @indianmonster1445 3 ปีที่แล้ว +1

    wow........finaly i got it this trick .....

  • @ahmadaltayeb1983
    @ahmadaltayeb1983 2 ปีที่แล้ว +2

    Sir, this code give me run time error 1004, and mark on line (.CurrentPage=a), i use office 2019 and 2021, please advice

  • @firozbaigmogal519
    @firozbaigmogal519 3 ปีที่แล้ว +1

    good morning sir
    Awesome!!! thank you very much
    how to create the button pls tell me

  • @jaimecastellanos87
    @jaimecastellanos87 3 ปีที่แล้ว +1

    This was amazing! But Im having issues on the .currentpage=a / Run-time error '1004': Unable to set the CurrentPage property of the PivotField Class. How could I fix it sir? Thank you

    • @dallinperkes8956
      @dallinperkes8956 3 ปีที่แล้ว +3

      The code from the video should work as long as you have multiple pivot tables, that are on the same sheet and have variables with the same names..... You would run into the error 1004 if you were trying to run this code on a singular pivot table.
      If this is the case, and you only have one pivot table, you can use the following code to do this same function......
      Sub FilterPageValue()
      Dim pvFld As PivotField
      Dim strFilter As String
      Set pvFld = ActiveSheet.PivotTables("PivotTableName").PivotFields("VariableName")
      strFilter = ActiveWorkbook.Sheets("SheetName").Range("Cell(ex. B4)").Value
      pvFld.CurrentPage = strFilter
      End Sub

  • @ganesha243
    @ganesha243 4 ปีที่แล้ว +1

    May i know, how to work on different worksheet pivot tables?

  • @technicalknowledge9128
    @technicalknowledge9128 2 ปีที่แล้ว

    sir one question i have cell value name like 50 and i want to add name one by one add to value field
    how to use please give me solution all name have column base and i want to name have a list and add to value by loop

  • @euridiceferreira569
    @euridiceferreira569 3 ปีที่แล้ว +2

    Good morning Sir. This a very important example. How would you modify the code if the pivot tables were in different sheets within the same workbook? Appreciate your response. Thank you.

    • @azmipangesti6470
      @azmipangesti6470 2 ปีที่แล้ว

      i have the same question with you. have you know the answer? if you know the answer i hope you can share to me. thank you

  • @heng9876
    @heng9876 4 ปีที่แล้ว +2

    May i know How do we change the filter based on just another cell value instead of clicking the button?

  • @armandorengifo8338
    @armandorengifo8338 3 ปีที่แล้ว +1

    Thanks a lot man!!

  • @ganesha243
    @ganesha243 4 ปีที่แล้ว +1

    How to protect worksheet while using dropdown list for pivot tables?

  • @Nainagrh0712
    @Nainagrh0712 3 ปีที่แล้ว

    my query, what to do if mutiple selection of cities, written in a list like, "Pune", "Delhi", "abc" then? it is not working in .CurrentPage = array("city1","city2"), lplease correct

    • @sahilkumar-iw2or
      @sahilkumar-iw2or 3 ปีที่แล้ว

      did you get it to work?

    • @cSR-71
      @cSR-71 2 ปีที่แล้ว

      if your data source is the same ... you can use a slicer. This method will not work for multiple selections.

  • @rushikeshkulkarni6075
    @rushikeshkulkarni6075 3 ปีที่แล้ว +1

    I have one pivot Table so then how i can use this code ?
    Bcoz the code is " for each pt in Activesheet pivot tables"

    • @dallinperkes8956
      @dallinperkes8956 3 ปีที่แล้ว

      Sub FilterPageValue()
      Dim pvFld As PivotField
      Dim strFilter As String
      Set pvFld = ActiveSheet.PivotTables("PivotTableName").PivotFields("VariableName")
      strFilter = ActiveWorkbook.Sheets("SheetName").Range("Cell(ex. B4)").Value
      pvFld.CurrentPage = strFilter
      End Sub

  • @luirm6946
    @luirm6946 3 ปีที่แล้ว

    Hi boss, is there any way you can share the code with multiple pivot fields selected

  • @danirasys
    @danirasys 4 ปีที่แล้ว +3

    I tried this, but I get an error on '.CurrentPage=a' Any idea why?

    • @ExcelDestination
      @ExcelDestination  4 ปีที่แล้ว +1

      without looking at complete code, I can't say anything. You can re check your sheet name and code.

    • @danirasys
      @danirasys 4 ปีที่แล้ว +1

      Sorry, I didn't realize I left that out! I will check again and share the full data when I get home from work.^^

    • @danirasys
      @danirasys 4 ปีที่แล้ว

      Here is what I have in my VBA:
      Public Sub UpdatePivotFields()
      Dim kt As String
      Dim ft As String
      Dim cl As String
      Dim ti As String
      Dim pt As PivotTable
      ThisWorkbook.Worksheets("Pivot Table").Activate
      kt = Worksheets("Pivot Table").Cells(2, 4).Value
      ft = Worksheets("Pivot Table").Cells(2, 5).Value
      cl = Worksheets("Pivot Table").Cells(2, 6).Value
      ti = Worksheets("Pivot Table").Cells(2, 7).Value
      For Each pt In ActiveSheet.PivotTables
      With pt.PivotFields("Korean Teacher")
      .ClearAllFilters
      .CurrentPage = kt
      End With
      Next
      For Each pt In ActiveSheet.PivotTables
      With pt.PivotFields("Foreign Teacher")
      .ClearAllFilters
      .CurrentPage = ft
      End With
      Next
      For Each pt In ActiveSheet.PivotTables
      With pt.PivotFields("Class")
      .ClearAllFilters
      .CurrentPage = cl
      End With
      Next
      For Each pt In ActiveSheet.PivotTables
      With pt.PivotFields("Time")
      .ClearAllFilters
      .CurrentPage = ti
      End With
      Next
      End Sub

    • @danirasys
      @danirasys 4 ปีที่แล้ว

      I appreciate your kindness to look at it. Thank you very much!

    • @stevemaxwell197
      @stevemaxwell197 4 ปีที่แล้ว

      Did you ever figure this out? I got this to work a month ago but when got back into today I get error in same place.

  • @hallinhko
    @hallinhko 4 ปีที่แล้ว +1

    Brilliant thank you very much

  • @sahilkumar-iw2or
    @sahilkumar-iw2or 3 ปีที่แล้ว

    @Excel Destination : Sir , if in the same case we have multiple cities in the data, like 1 row is delhi, Mumbai Other row is Delhi, other is Delhi,Pune. For this data how can we use the VBA? (like in excel we use logic in pivot ''CONTAINS'' Delhi)

  • @victoriardgs7397
    @victoriardgs7397 3 ปีที่แล้ว

    Hi there, I have tried this but it only works on sheets where my pivot tables are next to each other. Any way to slightly adapt this for pivot tables underneath each other ? Thanks !

    • @ankitshah6892
      @ankitshah6892 3 ปีที่แล้ว +1

      Hey! Did you get the solution for the problem mentioned? If yes plese share it me..

  • @jeronimoguardado7699
    @jeronimoguardado7699 4 ปีที่แล้ว +1

    Hi!! Awesome, but how can I clear all filters back??

    • @ExcelDestination
      @ExcelDestination  4 ปีที่แล้ว

      same code you can use to remove filter, try this and let me know if it works

    • @jeronimoguardado7699
      @jeronimoguardado7699 4 ปีที่แล้ว

      @@ExcelDestination Hi!!, but what do you mean by using the same? do i need to copy and paste all the same code and add a diferente button? Im not sure how to do it, because in this code Im telling excel to filter whenever I choose a City but what should I select to unfilter it?

  • @DCHAWADA07
    @DCHAWADA07 4 ปีที่แล้ว +1

    Sir awsome I was searching this from so many days, actually my office uses excel 2007 which doesn't have slicers so I need multiples selection to b done to be updated in pivot chart for dashboard , same as slicers work selecting multiples fields, pls help how do I do that ? 😞

    • @ExcelDestination
      @ExcelDestination  4 ปีที่แล้ว

      let me research for that

    • @DCHAWADA07
      @DCHAWADA07 4 ปีที่แล้ว

      @@ExcelDestination thank you so much sir, appreciate your response

  • @ganesha243
    @ganesha243 4 ปีที่แล้ว +1

    How to add dependent dropdown list ?

  • @alparkson5494
    @alparkson5494 2 ปีที่แล้ว

    This did not work for me. The error I received was "unable to get the pivotfields property of the pivottable class" for the field I was trying to filter on.

  • @BharatKumar-fh7ei
    @BharatKumar-fh7ei 4 ปีที่แล้ว +1

    It's very good.
    Instead of Pivot table, can we work in excel sheet?

  • @drewwingert831
    @drewwingert831 4 ปีที่แล้ว +1

    How would this work in Google Sheets? Is it possible?

    • @ExcelDestination
      @ExcelDestination  4 ปีที่แล้ว

      it is very much possible in google sheets

    • @drewwingert831
      @drewwingert831 4 ปีที่แล้ว

      @@ExcelDestination Thanks! Where is the VBA option in Google Sheets?

    • @drewwingert831
      @drewwingert831 4 ปีที่แล้ว

      I think I did some of it
      I recorded a Macro simialr to yours, but I am unsure how to create the button...any other instructions to do this in Google Sheets?

    • @drewwingert831
      @drewwingert831 4 ปีที่แล้ว

      @@ExcelDestination Any feedback on how?

  • @Just-My-Opinion
    @Just-My-Opinion 4 ปีที่แล้ว

    is it posible to have 2 drop down menus and filter 2 items instead of just one?

    • @manishvarshney601
      @manishvarshney601 3 ปีที่แล้ว

      Same question, is selecting 2 items in this VBA macro possible?

  • @123456789mumbai
    @123456789mumbai 2 ปีที่แล้ว

    Hello everyone, need your help if anyone can share some input
    I need to select 10 items in fieldname which are available in sheet 1 using vba. What code can be used?

  • @_bellecullen
    @_bellecullen 4 ปีที่แล้ว +1

    Thank you!!!!!

  • @mimiten7626
    @mimiten7626 3 ปีที่แล้ว

    How to change multiple table with different city values?

  • @EXCELTrainingg
    @EXCELTrainingg 4 ปีที่แล้ว +1

    Thank you so much

  • @EXCELTrainingg
    @EXCELTrainingg 4 ปีที่แล้ว +1

    Just Worthy

  • @ibadkhan481
    @ibadkhan481 4 ปีที่แล้ว +1

    It’s giving me error in “.Current Page= a” option please help

    • @ExcelDestination
      @ExcelDestination  4 ปีที่แล้ว

      not sure, you can share your file at exceldestination@gmail.com

    • @louisurban2342
      @louisurban2342 4 ปีที่แล้ว

      @@ExcelDestination Did this ever get resolved? I'm having the same issue

  • @djrobincooper
    @djrobincooper 4 ปีที่แล้ว

    Superb thank you

  • @indianmonster1445
    @indianmonster1445 3 ปีที่แล้ว +1

    sir please How to use formula in pivot tables ..........i don't understand...... please make this ......

    • @ExcelDestination
      @ExcelDestination  3 ปีที่แล้ว

      you can use calculated field option in pivot table. it is simple to use.

  • @Raj-ph1py
    @Raj-ph1py 4 ปีที่แล้ว +1

    Excellent tutorial

  • @obuleshkumar4866
    @obuleshkumar4866 3 ปีที่แล้ว

    vba code to select filter in 1 pivot table...Please

  • @zadirarivera3271
    @zadirarivera3271 4 ปีที่แล้ว +3

    This is a great video. I am trying to do it but I am getting Error 1004 Unable to set the CurrentPage property of the PivotField class. I haven't been able to find the error. This is my code, I would appreciate your help. Thanks
    Private Sub CommandButton1_Click()
    Dim a As String
    Dim pt As PivotTable
    ThisWorkbook.Worksheets("Reconciliation").Activate
    a = Worksheets("Reconciliation").Cells(4, 11).Value
    For Each pt In ActiveSheet.PivotTables
    With pt.PivotFields("Order")
    .ClearAllFilters
    .CurrentPage = a
    End With
    Next
    End Sub

    • @ExcelDestination
      @ExcelDestination  4 ปีที่แล้ว

      you can share your code at exceldestination@gmail.com, I will check and respond

    • @sanjaysamant4555
      @sanjaysamant4555 4 ปีที่แล้ว

      Excel Destination I’m getting same error and I have 12 tables...can you help???

  • @adolfmontaner9937
    @adolfmontaner9937 4 ปีที่แล้ว

    New subscriber here, btw great tutorial.
    I was trying to add another dropdown and here is the code but I am getting the error below:
    Run-time error '1004':
    Application-defined or object-defined error
    ----------------------------Code: --------------------------------------
    Private Sub Btn_FilterPivot_Click()
    Dim a As String
    Dim b As Double
    Dim pt As PivotTable
    ThisWorkbook.Worksheets("Ledger").Activate
    a = Worksheets("Ledger").Cells(19, 2).Value
    b = Worksheets("Ledger").Cells(19, 5).Value
    For Each pt In ActiveSheet.PivotTables
    With pt.PivotFields("WBS ID")
    .CurrentPage = a
    '.ClearAllFilters'
    End With
    Next
    For Each pt In ActiveSheet.PivotTables
    With pt.PivotFields("Accounting Month")
    .CurrentPage = b
    '.ClearAllFilters'
    End With
    Next
    End Sub
    ----------------------------Code: --------------------------------------

    • @adolfmontaner9937
      @adolfmontaner9937 4 ปีที่แล้ว

      I got it, this one works for me:
      Sub Btn_Filter()
      Dim a As String
      Dim b As String
      Dim c As String
      Dim pt As PivotTable
      Dim pt2 As PivotTable
      Dim pt3 As PivotTable
      ThisWorkbook.Worksheets("Ledger").Activate
      a = Worksheets("Ledger").Cells(3, 1).Value
      For Each pt In ActiveSheet.PivotTables
      With pt.PivotFields("WBS ID")
      .ClearAllFilters
      .CurrentPage = a

      End With
      Next
      b = Worksheets("Ledger").Cells(3, 2).Value
      For Each pt2 In ActiveSheet.PivotTables
      With pt2.PivotFields("Accounting year")
      .ClearAllFilters
      .CurrentPage = b

      End With
      Next
      c = Worksheets("Ledger").Cells(3, 3).Value
      For Each pt3 In ActiveSheet.PivotTables
      With pt3.PivotFields("Accounting month")
      .ClearAllFilters
      .CurrentPage = c

      End With
      Next
      End Sub

  • @lahaluke
    @lahaluke 4 ปีที่แล้ว

    Can anyone get this to work? Always gets stuck on the .currentpage=a
    Private Sub CommandButton1_Click()
    Dim a As String
    Dim pt As PivotTable
    ThisWorkbook.Worksheets("Analysis").Activate
    a = Worksheets("Analysis").Cells(1, 4).Value
    For Each pt In ActiveSheet.PivotTables
    With pt.PivotFields("City")
    .ClearAllFilters
    .CurrentPage = a
    End With

    Next
    End Sub

    • @lahaluke
      @lahaluke 4 ปีที่แล้ว +1

      It has to be a filter on the pivot table. It CANNOT be a row or column. Thanks.

    • @ExcelDestination
      @ExcelDestination  4 ปีที่แล้ว

      ok

  • @alexy6093
    @alexy6093 2 ปีที่แล้ว

    Wouldn't be easier if you just use a slicer?

    • @cSR-71
      @cSR-71 2 ปีที่แล้ว

      Only if the pivot tables are using the same data source. I can't because my pivot tables are from different data sources, though all have common 'cities'.
      Also need to make multiple selections, like a slicer can do.

  • @haryprasetyo4495
    @haryprasetyo4495 3 ปีที่แล้ว

    Hello Sir..i tried few times but seem not work and stop at .ClearAllFilters.. can you help..here with my script following your script
    Sub Button2_Click()
    Dim a As String
    Dim pt As PivotTable
    ThisWorkbook.Worksheets("Summary").Activate
    a = Worksheets("Summary").Cells(1, 8).Value
    For Each pt In ActiveSheet.PivotTables
    With pt.PivotFields("Time - Month")
    .ClearAllFilters
    .CurrentPage = a
    End With

    Next
    End Sub

  • @raghavendrabhat3668
    @raghavendrabhat3668 2 ปีที่แล้ว

    Sub Button1_Click()
    Dim a As String
    Dim pt As PivotTable
    ThisWorkbook.Worksheets("PIVOTS").Activate
    a = Worksheets("PIVOTS").Cells(2, 20).Value
    For Each pt In ActiveSheet.PivotTables
    With pt.PivotFields("Pole")
    .ClearAllFilters
    .CurrentPage = a
    End With
    Next
    End Sub
    Gettting error on line "With pt.pivot........

  • @udeychowdhury2529
    @udeychowdhury2529 3 ปีที่แล้ว +1

    Thanks for this