Help with Row Delete Macro


Posted by Ben O. on June 21, 2001 1:29 PM

Here's my row delete macro. It gets its values from a user form. It doesn't work most of the time, and when it works it works erractically. I don't get any errors, it just doesn't delete any rows. Can anyone tell me what's wrong?

Private Sub OKButton_Click()
Dim myRow As Integer
Dim myCol As Integer
myCol = ColBox.Value
cValue = ValueBox.Value
rBegin = rBeginBox.Value
rEnd = rEndBox.Value
For myRow = rBegin To rEnd Step 1
If Cells(myRow, myCol).Value = cValue Then
Cells(myRow, myCol).EntireRow.Delete
End If
Next myRow
Unload CondRowDel
End Sub

Thanks,

-Ben

Posted by Damon Ostrander on June 21, 2001 2:42 PM

Hi Ben,

I believe your problem is simply that your code
deletes rows in ascending order in the myRow loop.
Each time it deletes a row, all the row numbers
above that point decrement by one, so when the
loop goes to the next value it is actually jumping
two rows.

Simply change your loop to count backwards, i.e.:

For myRow = rEnd To rBegin Step -1

and that should take care of the problem.

Damon



Posted by Ben O. on June 22, 2001 6:51 AM

Thanks, Damon. That fixed it. (n/t)