Select Case - Loop - Delete Row

mmetzinger

Board Regular
Joined
Dec 30, 2010
Messages
61
OK, so I have the following code:

Code:
Dim oneCell As Range
Dim HeaderRange As String
        Range("Z1").Select
        Range(Selection, Selection.End(xlDown)).Select
        HeaderRange = Selection.Address
        
For Each oneCell In Range(HeaderRange)
If oneCell.Value = "" Then
GoTo skip
End If
    Select Case oneCell.Value
                Case "Policy Status"
                Case "PRMPY"
                Case "Issued- Not Paid"
                Case Else
                oneCell.EntireRow.Delete
    End Select
skip:
Next oneCell

which works great except for one issue. On the case else step when it deletes the row a problem is created with the loop in that the row that is after the row that is deleted moves up but the loop goes to the next row after it. All I need is a way to tell it that when it does the delete it also needs to step back one "oneCell" but i don't have a clue how to do this.

Any help would be greatly appreciated!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You need to loop backwards

Code:
Dim LR As Long, i As Long
LR = Range("Z" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    Select Case Range("Z" & i).Value
                Case "Policy Status"
                Case "PRMPY"
                Case "Issued- Not Paid"
                Case Else
                Rows(i).Delete
    End Select
Next i
 
Upvote 0
You need to loop backwards but you can't do that using For Each.

Try this.
Code:
Dim oneCell As Range

 For I = Range("Z" & Rows.Count).End(xlUp).Row To 1 Step -1        
   Set oneCell = Range("Z" & I

   Select Case oneCell.Value
          Case "Policy Status","PRMPY","Issued- Not Paid"
                Case Else
          oneCell.EntireRow.Delete
   End Select
 Next I
 
Upvote 0
This can be avoided by working backwards. And you don't need to select cells to work with them...

Code:
Sub Delete()
Dim Last_Row As Long
Dim i As Long
Application.ScreenUpdating = False
Last_Row = Range("Z1").End(xlDown).Row
For i = Last_Row To 1 Step -1     
        
        Select Case Cells(i, 26)
                    Case "Policy Status"
                    Case "PRMPY"
                    Case "Issued- Not Paid"
                    Case ""
                    Case Else
                    Cells(i, 26).EntireRow.Delete
        End Select
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for all of the replies. You all pointed out that I needed to work backwards which did resolve my issue.
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,055
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top