title: Excel VBA - when looping through files in a directory, how to skip to next file onError
body: <p>I'm looping through the files in a directory, where I need to open each file, do some manipulations and then close it.</p>
  
  <p>From time to time the Workbooks.Open fails with an error, and I simply want to display a MsgBox and then continue to the next file in the directory loop. But since the Error block is outside the loop, I can't just call 'Loop'... so how do I do this ?</p>
  
  <p>The test below immediately triggers an error that I can't call "Loop" without a "For"...</p>
  
  <pre><code>Sub test()
      Dim folderPath As String
      Dim filename As String
  
      folderPath = 'C:\myDirectory'
      filename = Dir(folderPath &amp; "*.xlsx")
  
      Do While filename &lt;&gt; ""
  
          On Error GoTo OpenFailedError
              Set wb = Workbooks.Open(folderPath &amp; filename)
          On Error GoTo 0
  
          filename = Dir
      Loop
  
  Exit Sub
  
  OpenFailedError:
      MsgBox ("Unable to open file " &amp; filenameString)
      Loop
  
  End Sub
  </code></pre>