title: How to Use Error Handling to Skip Loop Iterations
body: <p>I have a for loop where near the top I expect to throw an error trying to set a variable equal to a workbook. The error is returned because the workbook may or may not exist. The for loop loops over many workbooks and any number of them may or may not exist. the loop looks something like this</p>
  
  <pre><code>for i = 1 to x
       'get ready for to open the workbook
  
       set = myworkbook = workbooks.open("path\myworkbook" &amp; date)
  
       'do a bunch of stuff after I get the workbook
  
  next i
  </code></pre>
  
  <p>this works all fine and dandy, but if the workbook doesn't exist, then I get an error. Now I have tried a handful of error handling techniques. Ideally what happens is that if the workbook isn't found, it skips right down to the bottom of the loop and just goes into the next i.</p>
  
  <p>I have used goto statements to just jump to the bottom of the line, but that only works once and on the second pass through when the workbook doesn't exist, that throws an error. I have tried err.Clear after the the goto line so that the new error can be caught. I have tried goto -1 to clear the error but that doesn't work either. I have tried a variety of resume next statements but if the error doesn't occur because the workbooks are found, then that throws an error which I can't seem to handle. I check the error number before it starts the error handling routine wrapping the variable setting line to make sure that it shows 0 but it still throws the error.</p>
  
  <p>There seem to be a lot of methods to solve this problem and I think I've tried them all. This isn't a unique problem I'm certain and I just need a push in the right direction. Thanks for any help you guys might provide.</p>
  
  <p>Just so you don't think I haven't tried whatever solution you think of first, here are some other questions I've read:</p>
  
  <p><a href="https://stackoverflow.com/questions/19039657/for-loop-how-to-skip-iterations">For Loop, how to skip iterations</a></p>
  
  <p><a href="https://stackoverflow.com/questions/14158901/difference-between-on-error-goto-0-and-on-error-goto-1-vba/14159999#14159999">Difference between &#39;on error goto 0&#39; and &#39;on error goto -1&#39; -- VBA</a></p>
  
  <p><a href="https://stackoverflow.com/questions/15542747/on-error-goto-0-not-resetting-error-trapping">On Error Goto 0 not resetting error trapping</a></p>
  
  <p><a href="https://stackoverflow.com/questions/330937/access-vba-is-it-possible-to-reset-error-handling">Access VBA: Is it possible to reset error handling</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5895908/continue-for-loop">Continue For loop</a></p>
  
  <p><a href="https://stackoverflow.com/questions/35372946/error-handling-only-works-once">Error handling only works once</a></p>