michaelsmith559
Well-known Member
- Joined
- Oct 6, 2013
- Messages
- 881
- Office Version
- 2013
- 2007
I am using the below macro to delete rows that are less than 4 or greater than 9. The macro runs correctly except for deleting the correct row count. The macro is leaving selected ranges with row counts greater than 9. Here is the macro:
Code:
nlr = Cells(Rows.Count, 12).End(xlUp).Row
For i = nlr To 2 Step -1
myval = Cells(i, "L").Value
lr = Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = _
Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Offset(0, -5).Column
add1 = Columns(1).Find(What:=myval, LookIn:=xlValues, LookAt:=xlWhole).Row
xrow = add1
Do
If Cells(xrow + 1, 1).Value <> myval Then
add2 = xrow + 1
Exit Do
Else
xrow = xrow + 1
End If
Loop Until xrow = lastrow
add2 = xrow
Range(Cells(add1, 1), Cells(add2, lastcolumn)).Select
mycount = Selection.Rows.Count
If mycount < 4 Or mycount > 9 Then
Selection.Delete Shift:=xlUp
End If
Next i