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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,890
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Try this (untested):
Rich (BB code):
Error_Handler:
On Error GoTo 0
On Error GoTo Error_Handler_2
'rest of code
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,890
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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
 

Walker5

New Member
Joined
Mar 3, 2005
Messages
31

ADVERTISEMENT

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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

What error are you trying to catch?
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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
 

Walker5

New Member
Joined
Mar 3, 2005
Messages
31
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!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,707
Messages
5,833,241
Members
430,199
Latest member
Petty queen

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
Top