title: How to loop through all sheets and if cells match copy range to sheet?
body: <p>I have a workbook with many sheets. I want to consolidate the worksheets.</p>
  <p>Cell A13 in most of the sheets is different but for some they are the same.</p>
  <p>I want to loop through the sheets. If Cell A13 matches then copy rows 10 &amp; 11 into the first matching sheet in A10 and delete the other matching sheets.</p>
  <p>For example,</p>
  <p>Compare Sheet1 and Sheet2 -&gt; If cell A13 doesn't match compare the next sheet i.e. Compare Sheet2 and Sheet3 to check if they match.</p>
  <p>If Cell A13 in Sheet2 and Sheet3 match copy rows 10 &amp; 11 from Sheet3 into Sheet2 below Row 10 and delete Sheet3.</p>
  <p>Then compare Sheet2 to Sheet4 and so on.</p>
  <pre class="lang-vb prettyprint-override"><code>Sub WorksheetLoop()
  
      Dim WS_Count As Integer
      Dim I As Integer
  
      ' Set WS_Count equal to the number of worksheets in the active workbook.
      WS_Count = ActiveWorkbook.Worksheets.Count
  
      ' Begin the loop.
      For I = 1 To WS_Count
          If Worksheets(I).Range(&quot;A13&quot;).Value = Worksheets(I + 1).Range(&quot;A13&quot;) Then
              Worksheets(I + 1).Range(&quot;A7:L9&quot;).Copy
              Worksheets(I).Range(&quot;A10&quot;).PasteSpecial
              Worksheets(I + 1).Delete
          End If
      Next I
  
  End Sub
  </code></pre>
  <p>First of all I get an error when this finishes (I suspect because of the range).</p>
  <p>Secondly, it works when &quot;A13&quot; is matching for two sheets only. When there are multiple sheets that match it only processes the first matching sheet.</p>
  <p>Another issue is, if there are multiple matching sheets, how would I preserve all the ranges that are copied over into the first sheet so they are pasted below each other instead of being overwritten?</p>