title: VBA Excel Macro Delete Sheets With String
body: <p>I have used a bit of VBA code to search through all sheet names in a workbook for a certain string, lets call it <strong>"Text"</strong>. When it finds a sheet with that string it should delete that sheet. But lets say there are four sheets with <strong>"Text"</strong> in the name (named <strong>text 1</strong>, <strong>text 2</strong>, <strong>text 3</strong> and <strong>text 4</strong>), instead of deleting all four, it deletes <strong>Text 1</strong> and <strong>Text 3</strong>. It leaves the 2nd and 4th as non-deleted. Then if I call the macro again it deletes <strong>Text 2</strong> but leaves <strong>Text 4</strong>. Finally if I click it again it deletes Text 4. 
  I cannot work out why as it looks like it should work.</p>
  
  <pre><code>    Dim i As Integer, n As Integer
      n = ThisWorkbook.Worksheets.Count
      i = 1
      Application.DisplayAlerts = False
      Application.ScreenUpdating = False
      Do
          On Error Resume Next
          If InStr(1, Sheets(i).Name, "Text") Then Sheets(i).Delete
          On Error GoTo 0
          i = i + 1
      Loop Until i = n
      Application.DisplayAlerts = True
  
      Application.ScreenUpdating = True
  </code></pre>