title: Error handling in collection environment
body: <p>I have a macro that copies the content of a input table to an output table using the header names stored in a collection. Code:</p>
  
  <pre><code>Sub Process_Data()
  
  Dim rawSht As Worksheet
  Dim procSht As Worksheet
  Dim headers As Collection
  Dim c As Integer
  Dim v As Variant
  
  Set rawSht = ThisWorkbook.Worksheets("Backend - raw")
  Set procSht = ThisWorkbook.Worksheets("Backend - processed")
  
  Set headers = New Collection
  For c = 1 To rawSht.Cells(4, Columns.Count).End(xlToLeft).Column
      headers.Add c, rawSht.Cells(4, c).Text
  Next
  
  For c = 5 To 50
      On Error GoTo ErrorHandler
      rawCol = headers(procSht.Cells(8, c).Text)
      v = rawSht.Range(rawSht.Cells(5, rawCol), rawSht.Cells(Rows.Count, rawCol).End(xlUp)).Value2
      procSht.Cells(9, c).Resize(UBound(v, 1)).Value = v
  
  ErrorHandler:
      Next
  
  End Sub
  </code></pre>
  
  <p>I now tried to handle the error if I would insert a column in the output table, with a header not contained in the collection of headers from the input table. </p>
  
  <p>My solution with <code>On Error GoTo ErrorHandler</code> and then <code>ErrorHandler: Next</code> works if there is no more than 1 inserted column at a time (e.g. 2 or more inserted columns following each other returns an error). It also does not work if the inserted column is at the beginning or at the end of the output table.</p>
  
  <p>What I would like to do is: If the header is found in the collection, copy &amp; paste the data from input table, if the header is not found in the collection, go to the next header, if the next header is empty, stop the macro.</p>