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.
 

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,389
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,389
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,219
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,123,156
Messages
5,600,049
Members
414,357
Latest member
Gemma_R

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