+ Reply to Thread
Results 1 to 4 of 4

Thread: Handling pivot tables with macros - Filtering fields

  1. #1
    Forum Contributor
    Join Date
    02-07-2012
    Location
    Buenos Aires, Argentina
    MS-Off Ver
    Excel 2007, 2010
    Posts
    348

    Question Handling pivot tables with macros - Filtering fields

    Hello!
    How can I filter items from a certain field of a Pivot Table without having to use something like
    With ActiveSheet.PivotTables("Table1").PivotFields("Field1")
            .PivotItems("Item1").Visible = False
            .PivotItems("Item2").Visible = False
            .PivotItems("Item3").Visible = False
            .PivotItems("Item4").Visible = False
            .PivotItems("Item5").Visible = False
                            .
                            .
                            .
                            .
                            .
                            .
            .PivotItems("ItemN").Visible = False
    End With
    where every item has to be turned off one by one? Is it possible to hide them all and then use visible.true to show only the item I want to see? Keep in mind that the items names aren't as simple as shown in the code, so using a changing string to call each name inside a loop isn't possible.
    Thanks in advance
    Last edited by Pichingualas; 02-10-2012 at 11:33 AM.

  2. #2
    Forum Contributor
    Join Date
    02-07-2012
    Location
    Buenos Aires, Argentina
    MS-Off Ver
    Excel 2007, 2010
    Posts
    348

    Re: Handling pivot tables with macros - Filtering fields

    Dang I guess no one knows

  3. #3
    Forum Contributor
    Join Date
    02-07-2012
    Location
    Buenos Aires, Argentina
    MS-Off Ver
    Excel 2007, 2010
    Posts
    348

    Re: Handling pivot tables with macros - Filtering fields

    In case anyone else has this problem and needs a solution, I found a code which can be used, although for what I want I'm having to adapt it. The source I found it at is:

    http://www.contextures.com/xlPivot03.html

    I hope that helps someone.

  4. #4
    Forum Contributor
    Join Date
    02-07-2012
    Location
    Buenos Aires, Argentina
    MS-Off Ver
    Excel 2007, 2010
    Posts
    348

    Re: Handling pivot tables with macros - Filtering fields

    Okay, this is the code I came up with.

    Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, ByRef Cancel As Boolean)
        'Filters pivot tables associated to the item you click on
        Dim a As Integer 'Columns number
        Dim b As Long 'Rows number
        Dim x As Byte 'For knowing what stage of the filter is running
        Dim pt As PivotTable
        Dim pf As PivotField
        Dim pi As PivotItem
        Dim strPF As String 'Name of the pivot field to be filtered
        Dim strPI As String 'Only item to stay visible
        
        Application.ScreenUpdating = False
        
        If Sh.Name = "Clientes" Then
            strPF = "Razón social"
        ElseIf Sh.Name = "RRCC" Then
            strPF = "Representante comercial"
        ElseIf Sh.Name = "Productos" Then
            strPF = "Ejercicio/Período"
        Else
            Exit Sub
        End If
        
        If Target.Column = 1 And Target.Row <= Range("A1").CurrentRegion.Rows.Count Then
            a = Range("A1").CurrentRegion.Columns.Count
            b = Range("A1").CurrentRegion.Rows.Count
            If Target.Row <= 2 Then
                x = 1
                           
                If x = 1 Then
                    Sheets("TD MUsMP").Select
                End If
    DontFilterPT:
                Set pt = ActiveSheet.PivotTables(1)
                Application.DisplayAlerts = False
                On Error Resume Next
                With pf
                    .AutoSort xlManual, .SourceName
                    For Each pi In pf.PivotItems
                        pi.Visible = True
                    Next pi
                    .AutoSort xlAscending, .SourceName
                End With
                Application.DisplayAlerts = True
                x = x + 1
                If x = 2 Then
                    Sheets("TD CMN").Select
                    GoTo DontFilterPT
                End If
                On Error GoTo 0
            Else
                x = 1
                strPI = Target.Value            
                If x = 1 Then
                    Sheets("TD MUsMP").Select
                End If
    filterPT:
                Set pt = ActiveSheet.PivotTables(1)
                Set pf = pt.PivotFields(strPF)
                Application.DisplayAlerts = False
                On Error Resume Next
                With pf
                    .AutoSort xlManual, .SourceName
                    For Each pi In pf.PivotItems
                        pi.Visible = False
                        If pi.Value = strPI Then
                            pi.Visible = True
                        End If
                    Next pi
                    .AutoSort xlAscending, .SourceName
                End With
                Application.DisplayAlerts = True
                x = x + 1
                If x = 2 Then
                    Sheets("TD CMN").Select
                    GoTo filterPT
                End If
                On Error GoTo 0
            End If
            Cancel = True
        End If
        
        Sheets(Sh.Name).Select
        Application.ScreenUpdating = True
        
    End Sub
    It works perfectly.
    Last edited by Pichingualas; 02-10-2012 at 11:34 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0