On Error GoTo within On Error GoTo

Walker5

New Member
Joined
Mar 3, 2005
Messages
31
My second error handler doesn't work. I am getting a Run-time error from the line after "On Error GoTo Error_Handler_2" in this macro:

Sub Macro1()
If Sheets("Sheet5").PivotTables("PivotTable2").PivotFields("Type").PivotItems("a").Position = 1 Then x = "a"
On Error GoTo Error_Handler
If Sheets("Sheet5").PivotTables("PivotTable2").PivotFields("Type").PivotItems("e").Position = 2 Then x = "e"
Exit Sub
Error_Handler:
On Error GoTo Error_Handler_2
If Sheets("Sheet5").PivotTables("PivotTable2").PivotFields("Type").PivotItems("f").Position = 1 Then x = "f"

Error_Handler_2:
MsgBox "Unsolvable Error"
End Sub

I thought that a second "On Eror GoTo" would work within the first "On Eror GoTo" This is for a Pivot table that only has fields "a", "b" and "c", no "e" or "f" fields. Any thoughts would be greatly appreciated.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this (untested):
Rich (BB code):
Error_Handler:
On Error GoTo 0
On Error GoTo Error_Handler_2
'rest of code
 
Upvote 0
Since the statement producing the error is immediately before your second error handler try this:
Rich (BB code):
Error_Handler:
On Error GoTo 0
On Error Resume Next
If Sheets("Sheet5").PivotTables("PivotTable2").PivotFields("Type").PivotItems("f").Position = 1 Then x = "f"
MsgBox "Unsolvable Error"
End Sub
 
Upvote 0
Nope, still getting the same error. It's as if VBA doesn't recognize "On Error" because it is already within an "On Error" statement.
 
Upvote 0
What error are you trying to catch?
 
Upvote 0
Try:

Code:
Sub Macro1()
    If Sheets("Sheet5").PivotTables("PivotTable2").PivotFields("Type").PivotItems("a").Position = 1 Then x = "a"
    On Error GoTo Error_Handler
    If Sheets("Sheet5").PivotTables("PivotTable2").PivotFields("Type").PivotItems("e").Position = 2 Then x = "e"
    Exit Sub
Error_Handler:
    Resume Here
Here:
    On Error GoTo Error_Handler_2
    If Sheets("Sheet5").PivotTables("PivotTable2").PivotFields("Type").PivotItems("f").Position = 1 Then x = "f"
    Exit Sub
Error_Handler_2:
    MsgBox "Unsolvable Error"
End Sub
 
Upvote 0
Andrew,
It worked like a charm. You're the man and quite deserved of the tilte "MrExcel MVP." Thank you. I will file that in my "Excel things to remember" workbook along with about a million other things.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,213,521
Messages
6,114,109
Members
448,548
Latest member
harryls

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