Skip one worksheet and process the remaining works

2020-04-18 07:37发布

I was working with a vba code which remove all the rows which dont have word "Statement No" in it from all available worksheets. But anyhow i want it to skip the first worksheet and continue removing the rows from all other worksheets.

If you can help me with the below code so that it skips the first worksheet.

Sub doit()

Application.DisplayAlerts = False

Dim r As Long, lr As Long
Dim sh As Worksheet



For Each sh In Sheets

lr = sh.Cells(sh.Rows.Count, 1).End(xlUp).row
For r = lr To 1 Step -1
    If InStr(sh.Cells(r, 1), "Statement No") = 0 Then sh.Rows(r).Delete
Next r
Next

Application.DisplayAlerts = True

End Sub

Thank you All!

2条回答
Explosion°爆炸
2楼-- · 2020-04-18 07:40

Just tell it to ignore the sheet by checking each sheet name:

Sub doit()

    Application.DisplayAlerts = False

    Dim r As Long, lr As Long
    Dim sh As Worksheet

    For Each sh In Sheets
        If sh.Name <> "IgnoreThisSheet" Then
            lr = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row
            For r = lr To 1 Step -1
                If InStr(sh.Cells(r, 1), "Statement No") = 0 Then sh.Rows(r).Delete
            Next r
        End If
    Next

    Application.DisplayAlerts = True

End Sub

Edit: You may also want to change Sheets to WorkSheets in your For Each loop. A worksheet contains rows, columns, etc. A sheet can be a chart sheet, macro sheet or dialog sheet. http://blogs.msdn.com/b/frice/archive/2007/12/05/excel-s-worksheets-and-sheets-collection-what-s-the-difference.aspx

查看更多
Emotional °昔
3楼-- · 2020-04-18 07:55

If the sheet name is always going to be the same then you could use this ..

If sh.Name <> "Sheet 1" Then ' Or whatever the sheet is called
   .. more code
End If
查看更多
登录 后发表回答