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
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