Consulting

Results 1 to 6 of 6

Thread: Pivot table filter based on value using VBA code

  1. #1

    Post Pivot table filter based on value using VBA code

    Hi All,

    The given below code filter the Pivot table based on cell value. I created this in module. But I want to implement this in specific worksheet "Market_MU Summary",

    as I run this in "private sub procedure" with "change" event, the code just keep refreshing the pivots and doesn't stop even it hangs the excel.

    Please any help on this.

    Option Explicit
    
    Sub Worksheet_Change1111()
        Dim a As String
        Dim pt As PivotTable
        Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Market_MU Summary")
        Set pt = ws.PivotTables("PivotTable3")
    ' Activate the worksheet containing the PivotTable
        'ThisWorkbook.Worksheets("Market_MU Summary").Activate
    ' Get the value from cell C3
        a = Worksheets("Market_MU Summary").Cells(3, 33).Value
    ' Loop through all PivotTables in the active sheet
        For Each pt In ActiveSheet.PivotTables
            With pt.PivotFields("Uniq Count")
                ' Check if "Uniq Count" is a valid field in the PivotTable
                If .Orientation = xlPageField Then
                    .ClearAllFilters
                    .CurrentPage = a
                End If
            End With
        Next pt
    End Sub
    Last edited by Aussiebear; 09-30-2023 at 05:28 AM. Reason: Added code tags to supplied code

  2. #2
    whenever you make changes to your pivot, the Change event fires, that is the reason you are in an endless loop.
    you must first make sure to disable all events before making changes, then re-instate all events.

  3. #3
    Thank you sir for your reply, I am actually new to VBA and learning it, it is solved. Many many thanks again sir.
    Last edited by Raj.end300; 10-01-2023 at 12:33 AM.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,055
    Location
    Sorry raj.end300 but how is it solved? arnelgp raised a legitimate query to your post. Why was your solution?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    I just needed hint. Arnelgp provided me good. Here is the updated code :
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    Dim a As String
        Dim pt As PivotTable
        Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Market_MU Summary")
        Set pt = ws.PivotTables("PivotTable3")
    ' Activate the worksheet containing the PivotTable
        'ThisWorkbook.Worksheets("Market_MU Summary").Activate
    ' Get the value from cell C3
        a = Worksheets("Market_MU Summary").Cells(3, 33).Value
    ' Loop through all PivotTables in the active sheet
        For Each pt In ActiveSheet.PivotTables
            With pt.PivotFields("Uniq Count")
                ' Check if "Uniq Count" is a valid field in the PivotTable
                If .Orientation = xlPageField Then
                    .ClearAllFilters
                    .CurrentPage = a
                End If
            End With
        Next pt
    Application.EnableEvents = True
    End Sub
    Last edited by Aussiebear; 10-01-2023 at 03:52 PM. Reason: Added code tags to supplied code

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,055
    Location
    Thank you Raj.end300 for posting your solution
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •