Peter-Jan De Smedt
New Member
Hi
I've got below code (based on the recording of a macro) which filters 2 pivot tables on basis of the input of a cell and then sorts the pivot tables in ascending and descending order. The code works unless there is no data in either of the pivot tables. Can anybody please help me improve the code so that I no longer get the error message "Subscript out of range"?
Many thanks in advance for any help you can give me.
[pre]
[/pre]
I've got below code (based on the recording of a macro) which filters 2 pivot tables on basis of the input of a cell and then sorts the pivot tables in ascending and descending order. The code works unless there is no data in either of the pivot tables. Can anybody please help me improve the code so that I no longer get the error message "Subscript out of range"?
Many thanks in advance for any help you can give me.
[pre]
Code:
Private Sub CommandButton1_Click()
'filters the pivot table
Sheets("Pivottopbottom").PivotTables("PivotTable2").PivotFields("Key").ClearAllFilters
Sheets("Pivottopbottom").PivotTables("PivotTable2").PivotFields("Key").PivotFilters.Add _
Type:=xlCaptionContains, Value1:=Sheets("Pivottopbottom").Range("B3").Value
'sorts the pivot table
Range("C18").Select
Sheets("Pivottopbottom").PivotTables("PivotTable2").PivotFields("Key").AutoSort xlAscending _
, "Sum of € MM Depot", Sheets("Pivottopbottom").PivotTables("PivotTable2").PivotColumnAxis. _
PivotLines(1), 1
'filters the pivot table
Sheets("Pivottopbottom").PivotTables("PivotTable1").PivotFields("Key").ClearAllFilters
Sheets("Pivottopbottom").PivotTables("PivotTable1").PivotFields("Key").PivotFilters.Add _
Type:=xlCaptionContains, Value1:=Sheets("Pivottopbottom").Range("J3").Value
'sorts the pivot table
Range("L18").Select
Sheets("Pivottopbottom").PivotTables("PivotTable1").PivotFields("Key").AutoSort xlAscending _
, "Sum of € MM Depot", Sheets("Pivottopbottom").PivotTables("PivotTable1").PivotColumnAxis. _
PivotLines(1), 1
End Sub