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
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

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
 

Forum statistics

Threads
1,141,152
Messages
5,704,600
Members
421,359
Latest member
Edwardvanschothorst

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Top