# Code to delete rows

#### dennisli

##### Well-known Member
I want to perform deletion for row 9 to row 23 to keep the last row of each group.
For example, for range A9:A11, there are three 13888. But I just want keep the third one as the third row is the last row of the group.
For 96758, I just want to keep the fourth one as the fourth row is the last row of the group.

13888 200
13888 56
13888 45
96758 545
96758 56
96758 45
96758 55
97727 12
97727 110
97727 898
97727 1513
97727 1231
98427 500
98427 600
98427 900

In other words, after deleting, the remains should be

13888 45
96758 55
97727 1231
98427 900

I tried to use Find function code to do it but failed.
I would thank you all any ideas in advance.
Dennis

### Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},\$Z\$1:\$Z\$99,\$Y\$1:\$Y\$99),2,False) to lookup Y values to left of Z values.
Here is a sample of what I did to perform a similar function.

Sub row_delete()
' row_delete Macro
Application.ScreenUpdating = False
Dim i As Long
Dim MyRange As Range
Dim sh As Worksheet
Set sh = Worksheets("Claim Sorter")
Set MyRange = sh.Range("O3:O" & sh.Range("O4000").End(xlUp).Row)

For i = MyRange.Rows.Count To 2 Step -1 'set number of instances
With MyRange.Cells(i, 1)
If Left(.Value, 11) = Left(.Offset(-1, 0).Value, 11) Then
.Offset(-1, 0).Cells.Delete
End If
End With
Next i
Application.ScreenUpdating = True
End Sub

Your code is for the fixed interval. What I want to delete is different range for each group.

Try this. I had some more time to play with it a bit. I think this will work for you.

Sub row_delete()

Application.ScreenUpdating = False
Dim MyRange As Range
Dim sh As Worksheet

Set sh = Worksheets("Sheet1")
Set MyRange = sh.Range("A1:A" & sh.Range("A4000").End(xlUp).Row)

For i = MyRange.Rows.Count To 2 Step -1 'set number of instances
With MyRange.Cells(i, 1)
If Left(.Value, 5) = Left(.Offset(-1, 0).Value, 5) Then
.Offset(-1, 0).Cells.Delete
End If
End With
Next i
Application.ScreenUpdating = True
End Sub

Replies
3
Views
196
Replies
6
Views
453
Replies
4
Views
497
Replies
10
Views
451
Replies
29
Views
1K

1,219,905
Messages
6,150,901
Members
450,991
Latest member
ExcelDoer

### 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.

### Which adblocker are you using?

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

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