title: SAP and Excel: On Error GoTo Within On Error GoTo?
body: <p>I'm trying to go back and forth between my SAP GUI and my Excel spreadsheet. I have a list of tables that I want to view in SAP, pull the data from SAP, paste into Excel, and go to the next table. If that table does not exist in SAP, I want it to go to the next table (the table may not currently exist, but it might exist in the future, and I want this to be dynamic I do not want to hard code the table names). </p>
  
  <p>I already have one sequence of an On Error GoTo working, but say that the next table we want to reference also doesn't exist; that error would have to be handled.</p>
  
  <pre><code>Sub SAPEverything()
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  ans = MsgBox("Are you currently logged into SAP?", vbYesNoCancel)
  
  If ans = vbNo Then
      MsgBox ("Please log into SAP, then come back to this macro.")
      Exit Sub
  ElseIf ans = vbCancel Then
      Exit Sub
  ElseIf ans = vbYes Then
      frmSAP.Show
      frmSAP.Hide
  
      LastRow = Sheets("Sheet2").Cells(Rows.Count, 19).End(xlUp).Row
      CurrRow = 2
  
      For i = 2 To LastRow
          Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
          Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
          Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
          Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
          'Start the transaction to view a table
          session.StartTransaction "Transaction"
  
          session.findById("wnd[0]").maximize
          session.findById("wnd[0]/tbar[1]/btn[16]").press
          session.findById("wnd[0]/tbar[1]/btn[8]").press
          On Error GoTo HandlingIt
          session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectAll
          session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
          session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItemByText "Copy Text"
  
          Workbooks("WorkbookName").Activate
          Sheets("Sheet2").Select
          Cells(CurrRow, 2).Select
          ActiveSheet.Paste
          NewLastRow = Sheets("Sheet2").Cells(Rows.Count, 2).End(xlUp).Row
          For k = CurrRow To NewLastRow
              Sheets("Sheet2").Cells(k, 1).Value = Sheets("Sheet2").Cells(i, 19).Value
          Next k
          CurrRow = NewLastRow + 1
  
      Next i
  HandlingIt:
      currErr = i
      For i = currErr + 1 To LastRow
          Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
          Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
          Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
          Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
          'Start the transaction to view a table
          session.StartTransaction "Transaction"
  
          session.findById("wnd[0]").maximize
          session.findById("wnd[0]/tbar[1]/btn[16]").press
  
          session.findById("wnd[0]/tbar[1]/btn[8]").press
          On Error GoTo HandlingIt
          session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectAll
          session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
          session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItemByText "Copy Text"
  
          Workbooks("WorkbookName").Activate
          Sheets("Sheet2").Select
          Cells(CurrRow, 2).Select
          ActiveSheet.Paste
          NewLastRow = Sheets("Sheet2").Cells(Rows.Count, 2).End(xlUp).Row
          For k = CurrRow To NewLastRow
              Sheets("Sheet2").Cells(k, 1).Value = Sheets("Sheet2").Cells(i, 19).Value
          Next k
          CurrRow = NewLastRow + 1
  
      Next i
  End If
  </code></pre>
  
  <p>Is there any possible way to refer to another On Error GoTo section of the code once I'm already in a On Error GoTo section? Or even go back to the beginning of the current On Error GoTo section?</p>