Linking cells to multiple criteria Pivot table filtre

ExcelBusCtrl

New Member
Joined
Nov 29, 2017
Messages
4
Hi,

I have a pivot table that is based on a data set which is updated every month. The pivot table has a multiple criteria filtre in order to select the month ie 1/1/2017,2/1/2017,etc.

I want to select the months from an external cell, meaning that if I type in 5 that cell the pivot table filtre should include all the months from January to Mai.

I manage to do that in the enclosed code, but is there a more rational way to write this code than with 12 If.. Then statements? Or is there another way of solving this problem?

Thanks for your help:)

Code:
Sub test_pivot()

    ActiveSheet.PivotTables("Pivottable1").PivotFields("Month").CurrentPage = _
        "(All)"
     
   If Range("periode") = 1 Then
   ActiveSheet.PivotTables("Pivottable1").PivotFields("Month").CurrentPage = "(All)"
    With ActiveSheet.PivotTables("Pivottable1").PivotFields("Month")
        .PivotItems("1/1/2017").Visible = True
        .PivotItems("2/1/2017").Visible = False
        .PivotItems("3/1/2017").Visible = False
        .PivotItems("4/1/2017").Visible = False
        .PivotItems("5/1/2017").Visible = False
        .PivotItems("6/1/2017").Visible = False
        .PivotItems("7/1/2017").Visible = False
        .PivotItems("8/1/2017").Visible = False
        .PivotItems("9/1/2017").Visible = False
        .PivotItems("10/1/2017").Visible = False
        .PivotItems("11/1/2017").Visible = False
        .PivotItems("12/1/2017").Visible = False
    Range("H13").Select
    End With
   End If
    
   If Range("periode") = 2 Then
   ActiveSheet.PivotTables("Pivottable1").PivotFields("Month").CurrentPage = "(All)"
    With ActiveSheet.PivotTables("Pivottable1").PivotFields("Month")
        .PivotItems("1/1/2017").Visible = True
        .PivotItems("2/1/2017").Visible = True
        .PivotItems("3/1/2017").Visible = False
        .PivotItems("4/1/2017").Visible = False
        .PivotItems("5/1/2017").Visible = False
        .PivotItems("6/1/2017").Visible = False
        .PivotItems("7/1/2017").Visible = False
        .PivotItems("8/1/2017").Visible = False
        .PivotItems("9/1/2017").Visible = False
        .PivotItems("10/1/2017").Visible = False
        .PivotItems("11/1/2017").Visible = False
        .PivotItems("12/1/2017").Visible = False
    Range("H13").Select
    End With
 End If
    If Range("periode") = 3 Then
   ActiveSheet.PivotTables("Pivottable1").PivotFields("Month").CurrentPage = "(All)"
    With ActiveSheet.PivotTables("Pivottable1").PivotFields("Month")
        .PivotItems("1/1/2017").Visible = True
        .PivotItems("2/1/2017").Visible = True
        .PivotItems("3/1/2017").Visible = True
        .PivotItems("4/1/2017").Visible = False
        .PivotItems("5/1/2017").Visible = False
        .PivotItems("6/1/2017").Visible = False
        .PivotItems("7/1/2017").Visible = False
        .PivotItems("8/1/2017").Visible = False
        .PivotItems("9/1/2017").Visible = False
        .PivotItems("10/1/2017").Visible = False
        .PivotItems("11/1/2017").Visible = False
        .PivotItems("12/1/2017").Visible = False
    Range("H13").Select
    End With
    End If
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Something like this?
Code:
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Months").ClearAllFilters

    ActiveSheet.PivotTables("PivotTable1").PivotFields("Month").PivotFilters.Add2 _
        Type:=xlCaptionIsLessThanOrEqualTo, Value1:=Sheets("Sheet1").Range("A1").Value
 
Upvote 0
Hi AFPathfinder,
I dont manage t get that code to work. At the second line I get "Run time error 1004."

Thanks for your contribution anyway!
 
Upvote 0
Did you modify the Value1 bit to match the sheet and cell you are putting the month value? I'm thinking the error might be caused by the sheet reference if Sheet1 doesn't exist.
 
Upvote 0
Hi - yes I did, but still no luck. Anyways, it works with the initial code I posted, so I'll just stick with that. Thanks for your efforts!
 
Upvote 0
Maybe something like this

Code:
Sub test_pivot2()
    Dim pi As PivotItem
    
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Month").ClearAllFilters
    If Range("periode") > 0 Then
        For Each pi In ActiveSheet.PivotTables("Pivottable1").PivotFields("Month").PivotItems
            If Month(DateValue(Format(pi, "mm/dd/yyyy"))) > Range("periode") Then pi.Visible = False
        Next pi
    End If
End Sub

M.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top