title: Loop Stops due to an IF Statement
body: <p>I created this script that allows me to change multiple dates within SAPs DEP - Production envirnoment VA02, and add an attachment (usually an email) as a proof of request.</p>
  <p>To deal with the multiple and unexpected pop-up windows that may appear, I introduced an error handler. My present issue is that, once the order has had the date changed, the attachment added and the whole process saved, it seems that the script goes into the Error Handler for some reason and gets stuck there.</p>
  <p>Sometimes a pop-up window appears when the process is completed. However, the EH should take care of it. It looks like the function goes through all the IFs written there even if it does not need to.</p>
  <p>I know the problem is within the EH, however, I do not know what is the root cause. I am thinking it might be the way is structured. Anyone has an idea?</p>
  <pre><code>Sub Roller()
      
      Dim ws          As Worksheet
      Dim i           As Long
      Dim maxi        As String
      
      Set ws = Excel.ThisWorkbook.Worksheets(&quot;Roll&quot;)
      i = 2
      maxi = ws.Cells(Rows.Count, 1).End(xlUp).Row
      
      Do While i &lt; maxi + 1
          
          On Error GoTo PopUpHandler
          
          Set SapGuiAuto = GetObject(&quot;SAPGUI&quot;)        '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)
          
          If Cells(4, 10) = &quot;YES&quot; Then
          
              If Cells(2, 5) = &quot;&quot; Then
              
                          session.StartTransaction &quot;va02&quot; 'To directly Open a SAP transaction
                          session.findById(&quot;wnd[0]/usr/ctxtVBAK-VBELN&quot;).Text = Cells(i, 1)
                          session.findById(&quot;wnd[0]&quot;).sendVKey 0
                          
                          session.findById(&quot;wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/btnBT_HEAD&quot;).press
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\16&quot;).Select
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\16/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9100/ctxtGX_VBAK-VDATU&quot;).Text = Cells(i, 2)
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\16/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9100/ctxtGX_VBAK-ZZWADAT&quot;).SetFocus
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\16/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9100/ctxtGX_VBAK-ZZWADAT&quot;).caretPosition = 0
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17&quot;).Select
                                      
                          session.findById(&quot;wnd[0]&quot;).sendVKey 11
                                      
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9200/tblSAPLZV444_PRIMARYTC_RC/ctxtGX_REASON_CODES-REASON_CODE[5,0]&quot;).Text = Cells(i, 3)
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9200/tblSAPLZV444_PRIMARYTC_RC/ctxtGX_REASON_CODES-REASON_CODE[5,0]&quot;).caretPosition = 4
                          session.findById(&quot;wnd[0]/titl/shellcont/shell&quot;).PressContextButton &quot;%GOS_TOOLBOX&quot;
                          session.findById(&quot;wnd[0]/titl/shellcont/shell&quot;).SelectContextMenuItem &quot;%GOS_PCATTA_CREA&quot;
                          session.findById(&quot;wnd[1]/usr/ctxtDY_PATH&quot;).Text = Cells(2, 7)
                          session.findById(&quot;wnd[1]/usr/ctxtDY_FILENAME&quot;).Text = Cells(i, 4)
                          session.findById(&quot;wnd[1]/tbar[0]/btn[0]&quot;).press
                          session.findById(&quot;wnd[0]&quot;).sendVKey 11
              
              Else
                          session.StartTransaction &quot;va02&quot;        'To directly Open a SAP transaction
                          session.findById(&quot;wnd[0]/usr/ctxtVBAK-VBELN&quot;).Text = Cells(i, 1)
                          session.findById(&quot;wnd[0]&quot;).sendVKey 0
                          session.findById(&quot;wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/btnBT_HEAD&quot;).press
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\16&quot;).Select
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\16/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9100/ctxtGX_VBAK-VDATU&quot;).Text = Cells(i, 2)
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17&quot;).Select
                          session.findById(&quot;wnd[0]&quot;).sendVKey 11
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9200/tblSAPLZV444_PRIMARYTC_RC/ctxtGX_REASON_CODES-REASON_CODE[5,0]&quot;).Text = Cells(i, 4)
                          session.findById(&quot;wnd[0]&quot;).sendVKey 0
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9200/tblSAPLZV444_PRIMARYTC_RC/ctxtGX_REASON_CODES-TAGGED_ITEMS[6,0]&quot;).Text = Cells(i, 5)
                          session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9200/tblSAPLZV444_PRIMARYTC_RC/txtGX_REASON_CODES-TAGGED_QTY[7,0]&quot;).Text = Cells(i, 6)
                          session.findById(&quot;wnd[0]/titl/shellcont/shell&quot;).PressContextButton &quot;%GOS_TOOLBOX&quot;
                          session.findById(&quot;wnd[0]/titl/shellcont/shell&quot;).SelectContextMenuItem &quot;%GOS_PCATTA_CREA&quot;
                          session.findById(&quot;wnd[1]/usr/ctxtDY_PATH&quot;).Text = Cells(2, 10)
                          session.findById(&quot;wnd[1]/usr/ctxtDY_FILENAME&quot;).Text = Cells(i, 7)
                          session.findById(&quot;wnd[1]/tbar[0]/btn[0]&quot;).press
                          session.findById(&quot;wnd[0]&quot;).sendVKey 11
                          
                              If session.ActiveWindow.Name = &quot;wnd[1]&quot; Then
                              session.findById(&quot;wnd[1]/usr/btnZSPOP_PRIMARY-OPTION1&quot;).press        'Outstanding issues present
                              End If
                              
              End If
                  
          Else
              
              session.StartTransaction &quot;va02&quot;        'To directly Open a SAP transaction
              session.findById(&quot;wnd[0]/usr/ctxtVBAK-VBELN&quot;).Text = Cells(i, 1)
              session.findById(&quot;wnd[0]&quot;).sendVKey 0
              session.findById(&quot;wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/subSUBSCREEN_BUTTONS:SAPMV45A:4050/btnBT_MKAL&quot;).press
              session.findById(&quot;wnd[0]/mbar/menu[1]/menu[1]/menu[3]&quot;).Select
              session.findById(&quot;wnd[1]/usr/ctxtRV45A-S_ETDAT&quot;).Text = Cells(i, 2)
              session.findById(&quot;wnd[1]/usr/ctxtRV45A-S_EZEIT&quot;).SetFocus
              session.findById(&quot;wnd[1]/usr/ctxtRV45A-S_EZEIT&quot;).caretPosition = 0
              session.findById(&quot;wnd[1]/tbar[0]/btn[7]&quot;).press
              
              session.findById(&quot;wnd[0]/titl/shellcont/shell&quot;).PressContextButton &quot;%GOS_TOOLBOX&quot;
              session.findById(&quot;wnd[0]/titl/shellcont/shell&quot;).SelectContextMenuItem &quot;%GOS_PCATTA_CREA&quot;
              session.findById(&quot;wnd[1]/usr/ctxtDY_PATH&quot;).Text = Cells(2, 7)
              session.findById(&quot;wnd[1]/usr/ctxtDY_FILENAME&quot;).Text = Cells(i, 4)
              session.findById(&quot;wnd[1]/tbar[0]/btn[0]&quot;).press
              session.findById(&quot;wnd[0]&quot;).sendVKey 11
              session.findById(&quot;wnd[0]&quot;).sendVKey 11
              session.findById(&quot;wnd[1]/usr/tblSAPLZV01_IBERIATC_OTIFVAL/ctxtX_OTIFVAL-RCODE[3,0]&quot;).Text = Cells(i, 3)
              session.findById(&quot;wnd[1]/tbar[0]/btn[8]&quot;).press
              
          End If
          
          Range(&quot;H&quot; &amp; i) = &quot;RDD updated And attachment added.&quot;
          
          i = i + 1
          
      Loop
      
      MsgBox &quot;RDDs have been updated accordingly To the request.&quot;
      
      Exit Sub
      
  PopUpHandler:
      
      If session.ActiveWindow.Name = &quot;wnd[1]&quot; Then
          session.findById(&quot;wnd[1]/tbar[0]/btn[0]&quot;).press        'Consider subsequent documents pop-up
      End If
      
          If session.ActiveWindow.Name = &quot;wnd[1]&quot; Then
          session.findById(&quot;wnd[1]/usr/btnZSPOP_PRIMARY-OPTION1&quot;).press        'Outstanding issues present
      End If
      
      If session.ActiveWindow.Name = &quot;wnd[2]&quot; Then
          session.findById(&quot;wnd[2]/tbar[0]/btn[0]&quot;).press        'Credit check (maximum % for open items exceeded pop-up
      End If
      
      If session.ActiveWindow.Name = &quot;wnd[2]&quot; Then
          session.findById(&quot;wnd[2]/tbar[0]/btn[0]&quot;).press        'Consider subsequent documents pop-up
      End If
          
      Resume
      
  End Sub ```
  </code></pre>