+ Reply to Thread
Results 1 to 3 of 3

Macro to filter pivot tables

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-25-2013
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    150

    Macro to filter pivot tables

    Hi All,

    I'm new to macros and I am completely stuck. I am trying to filter a pivot table based on a date range using a macro. I have two cells where the start date and the end date will be inputted (using data validation) which will make up the date range. I've scoured through the forums and found a code that works, but it is written for several PTs which all have the same filter name. My problem is that I have several PTs but with different filter names (e.g. some are called "Date" others "PeriodDate"). Can anyone help me adapt this code (or any other which would do the trick) so it will run on only a single PT?

    Public Sub Filter2()
        Application.ScreenUpdating = True
        
        Dim StartDate As Date, EndDate As Date
    
        StartDate = ActiveSheet.Range("B1").Value
        EndDate = ActiveSheet.Range("C1").Value
        'takes the Date values from the sheet
        
        'If there are no values then Gives them default values
        If StartDate = 0 Then
            MsgBox "Date Missing"
            Exit Sub
        End If
        If EndDate = 0 Then
            MsgBox "Date Missing"
            Exit Sub
        End If
        
        Dim PT As PivotTable
        Dim pf As PivotField
        Dim pi As PivotItem
        
        For Each PT In ActiveSheet.PivotTables
            'Filters The dates between StartDate and EndDate in all pivot tables
            'in the active sheet
            PT.PivotFields("Date").ClearAllFilters
            'goes through each pivot table on the sheet
            Set pf = PT.PivotFields("Date")
            For Each pi In pf.PivotItems
                'goes through every item in the field "Date"
                If Not pi.Name = "(blank)" Then
                    'ignores blanks and then checks if date is in range
                    If pi.Value < StartDate Or pi.Value > EndDate Then
                        pi.Visible = False
                    Else
                        pi.Visible = True
                    End If
                End If
    
            Next pi
        Next PT
        
        Application.ScreenUpdating = True
    End Sub
    Thanks in advance,

    Darrel

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: Macro to filter pivot tables

    The code that starts with the line "for each PT" and ends with the line "next PT" is especially designed to work with every pivot table on your sheet. You need to replace this with code that refers to your single pivot table.

    Replace the line that starts "for each...." With
    set PT = THE NAME OF YOUR PIVOT TABLE HERE
    Replace the line "next PT" with
    set PT = nothing
    To find out the full name of your pivot table, i suggest the following
    Turn on the macro recorder
    Click in the pivot table
    Click refresh
    Stop the macro recorder
    Look at the code - it will show you the full name of the pivot table. Use that name where indicated above.

  3. #3
    Forum Contributor
    Join Date
    04-25-2013
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: Macro to filter pivot tables

    Thanks so much Mallycat

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1