Delete a Row Based on 2 Rows

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
598
Office Version
  1. 2019
Platform
  1. Windows
I have a list of data that I am building a macro to cleanup. In my first column I have values that range from 2 to 8.
This has already been filtered from highest to lowest so the2’s are at the bottom and so on…
I need to delete every row except the 8’s and the first rowof 7. There will always be more than one row of 7, but I only need to keep one.
My first thought was to use this code:
Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]finalrow = Cells(Rows.Count, 1).End(xlUp).Row[FONT=Times New Roman][SIZE=3][COLOR=#000000]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]For i = finalrowTo 2 Step -1[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]If Cells(i, 2)<> 8 And Cells.Offset(Cells(i, 2), -1, 0).Value = 8 Then[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Cells(i,1).EntireRow.Delete[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Else[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]'donothing[/COLOR][/SIZE][/FONT]

[FONT=Calibri][SIZE=3][COLOR=#000000]End If[FONT=Times New Roman][SIZE=3][COLOR=#000000][FONT=Calibri]
[SIZE=3][COLOR=#000000]Next i

[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]


But it doesn’t seem to do what I want it to do. How can I savethe last row where the value is 7 and all the 8’s?

thanks,





 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
How about
Code:
If Cells(i - 1, 2).Value <> 8 Then
 
Upvote 0
Hi, you could try:

Code:
Dim firstrow As Long
Dim lastrow As Long

firstrow = Cells.Find("7", searchorder:=xlByRows, searchdirection:=xlNext).Row
lastrow = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

Range("B" & firstrow + 1 & ":A" & lastrow).EntireRow.Delete


Edit: Fluff beat me to it again :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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
Back
Top