• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Help needed with VBA code

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]
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
[/pre]
 
Hi Suresh

Thanks for your reply. I found the answer in the meantime. This is how I amended the code should you be interested.

'sorts depot table largest to smallest
Range("C16:E19").Select
ActiveWorkbook.Worksheets("X").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("X").Sort.SortFields.Add Key:=Range( _
"C19:E19"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("X").Sort
.SetRange Range("C16:E19")
.Header = xlGuess
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
'sorts fee table largets to smallest
Range("C31:E34").Select
ActiveWorkbook.Worksheets("X").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("X").Sort.SortFields.Add Key:=Range( _
"C34:E34"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("X").Sort
.SetRange Range("C31:E34")
.Header = xlGuess
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
Range("a3").Select
End With
'selects pivot won top 5 depot
Sheets("S").PivotTables("PivotTable1").PivotFields("Key").ClearAllFilters
Sheets("S").PivotTables("PivotTable1").PivotFields("Key").PivotFilters.Add _
Type:=xlCaptionContains, Value1:=Sheets("S").Range("B4").Value
'if pivot won emppty macro goes to pivot two
On Error GoTo Error_Handler
'sorts pivot won
Sheets("S").PivotTables("PivotTable1").PivotFields("Key").AutoSort xlAscending _
, "Sum of ", Sheets("S").PivotTables("PivotTable1").PivotColumnAxis. _
PivotLines(1), 1
'intermediate step to enable 2nd error handler which is ohterwise ignored by macro
Error_Handler:
Resume Here
Here:
'selects pivot lost bottom 5 depot
Sheets("S").PivotTables("PivotTable2").PivotFields("Key").ClearAllFilters
Sheets("S").PivotTables("PivotTable2").PivotFields("Key").PivotFilters.Add _
Type:=xlCaptionContains, Value1:=Sheets("S").Range("B4").Value
'if pivot lost empty macro errorhandler 2
On Error GoTo Error_Handler_2
Sheets("S").PivotTables("PivotTable2").PivotFields("Key").AutoSort xlAscending _
, "Sum of ", Sheets("S").PivotTables("PivotTable2").PivotColumnAxis. _
PivotLines(1), 1
Error_Handler_2:
Resume Here_2
Here_2:
'selects pivot won top 5 revenue
Sheets("S").PivotTables("PivotTable3").PivotFields("Key").ClearAllFilters
Sheets("PS").PivotTables("PivotTable3").PivotFields("Key").PivotFilters.Add _
Type:=xlCaptionContains, Value1:=Sheets("S").Range("B4").Value
On Error GoTo Error_Handler_3

'sorts pivot won top 5 revenue
Sheets("S").PivotTables("PivotTable3").PivotFields("Key").AutoSort xlAscending _
, "Sum of ", Sheets("S").PivotTables("PivotTable3").PivotColumnAxis. _
PivotLines(1), 1
Error_Handler_3:
Resume Here_3
Here_3:
'selects pivot lost bottom 5 revenue
Sheets("S").PivotTables("PivotTable4").PivotFields("Key").ClearAllFilters
Sheets("PS").PivotTables("PivotTable4").PivotFields("Key").PivotFilters.Add _
Type:=xlCaptionContains, Value1:=Sheets("S").Range("B4").Value
On Error GoTo Error_Handler_4
'sorts pivot lost bottom 5 revenue
Sheets("S").PivotTables("PivotTable4").PivotFields("Key").AutoSort xlAscending _
, "Sum of ", Sheets("S").PivotTables("PivotTable4").PivotColumnAxis. _
PivotLines(1), 1
Error_Handler_4:
If Application.WorksheetFunction.CountA(Sheets("S").Range("C18:L18:R18:V18")) = 0 Then
MsgBox "SSSS"
Exit Sub
End If
 
Back
Top