Delete Rows Button

M

Matt

I'm using this code to delete all the rows that have "RG" in the A
column. It should run through the entire range A1:A50 when I click a
userform button. But instead of deleting them all with one click, its
just deleting one of the "RG" rows each time I click the button (or
even a few of the rows, but not all). Can anyone help?

Private Sub RunReport_Click()
For Each dept In Range("A1:A50")
If dept.Value = "RG" Then
Rows(dept.Row).Delete
End If
Next dept
End Sub
 
F

FSt1

hi
that is because you are going down the list and with each delete, the data
shifts up one row which causes the for next loop to skip over the next row.
run your code in step mode and you'll see.
in this situation it is better to go UP the list to avoid skipping rows.
Private Sub RunReport_Click()
Dim r As Long
For r = 50 To 1 Step -1 'the rows
If Cells(r, "A").Value = "RG" Then 'range("A1:A50")
Rows(r).Delete
End If
Next r
End Sub

regards
FSt1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top