Code to delete rows

dennisli

Well-known Member
Joined
Feb 20, 2004
Messages
1,070
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
 

Some videos you may like

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Vangaard

New Member
Joined
Oct 19, 2005
Messages
35
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
 

dennisli

Well-known Member
Joined
Feb 20, 2004
Messages
1,070
Your code is for the fixed interval. What I want to delete is different range for each group.
 

Vangaard

New Member
Joined
Oct 19, 2005
Messages
35
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
 

Watch MrExcel Video

Forum statistics

Threads
1,111,491
Messages
5,541,034
Members
410,542
Latest member
GDiddy1984
Top