Results 1 to 5 of 5

Thread: Filter Pivot Table Based On Selected Value And Show All Results Though VBA

  1. #1

    Smile Filter Pivot Table Based On Selected Value And Show All Results Though VBA

    Hi All,

    Please find the excel file in which you can filter your result in pivot table according to dropdown selection and find the code give below:

    Code:
    Sub ShowAll()
    
        Dim pt As PivotTable, pi As PivotItem
        Dim xlCalc As XlCalculation
        
        Set pt = Sheet1.PivotTables("PivotTable1")
        
        pt.ManualUpdate = True
    
        With Application
            xlCalc = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False '
        End With
        
        On Error Resume Next
        
        For Each pi In pt.PivotFields("StaffCode").PivotItems
            pi.Visible = True
        Next pi
        
        On Error GoTo 0
    
        pt.ManualUpdate = False
    
        With Application
            .Calculation = xlCalc
            .ScreenUpdating = True
        End With
    
    End Sub
    
    
    Sub ShowPivotFilterResult()
    
        Dim pt As PivotTable, pi As PivotItem
        Dim xlCalc As XlCalculation
        
        
        Set pt = Sheet1.PivotTables("PivotTable1")
        
        pt.ManualUpdate = True
        
        With Application
            xlCalc = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False '
        End With
        
        On Error Resume Next
        
        For Each pi In pt.PivotFields("StaffCode").PivotItems
            pi.Visible = True
        Next pi
        
        On Error GoTo 0
    
    
        On Error Resume Next
        
        For Each pi In pt.PivotFields("StaffCode").PivotItems
            If pi.Value = Range("rngVALUE").Value Then
                pi.Visible = True
            Else
                pi.Visible = False
            End If
        Next pi
        
        On Error GoTo 0
    
        pt.ManualUpdate = False
    
        With Application
            .Calculation = xlCalc
            .ScreenUpdating = True
        End With
    
    End Sub
    Attached Files Attached Files
    Last edited by hanishgautam; 07-08-2013 at 09:09 PM. Reason: Code Tags Added

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Thanks Hanish. You can wrap your code using the code tags. To use code tags, either select your code and press the code tag button in the editor group, or type [Code]your code here[\Code] and use a forward slash instead of backward.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Sure I will remember next time whenever i will post...

  4. #4
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    13
    This is a nonsense item.
    Filtering can be accomplished in a table (listobject).
    The sum of ID's is nonsense.

  5. #5
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    snb, I tend to agree to your point (or concern, rather) regarding the above post, particularly since the pivot table is almost identical to a listobject on a sheet, and to filter a listobject, it doesn't take elaborate coding. I guess hanishgautam was just trying to showcase how one can filter a pivot table / pivot item.

    In addition, I agree that Sum of ID doesn't feel logical at all. Again, the intent was probably just what I presumed above. Having said that, I would put it this way - hanishgautam could have used a more apt data example to showcase what he was trying to.

    Let's just call it fair for this one.

    hanishgautam, snb is a very skilled and admirable person among such forums, and I am sure you'll appreciate the gravity of snb's mind in this above post of yours. Let's just say snb likes to run a tight ship when it comes to coding and applying it to scenarios.

    Cheers gentlemen.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Extract Selected Data from Excel - Advanced Filter
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 2
    Last Post: 01-10-2014, 10:49 AM
  2. Replace Pivot Table Source Range with New Address VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 1
    Last Post: 04-25-2013, 07:51 PM
  3. Replies: 3
    Last Post: 03-05-2013, 03:57 PM
  4. Filter more than one pivot table at one time
    By larryt1940 in forum Excel Help
    Replies: 8
    Last Post: 05-04-2012, 06:45 PM
  5. Replies: 9
    Last Post: 03-13-2012, 01:27 PM

Posting Permissions

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