Macro to delete all rows following a rule

richiwatts

Board Regular
Joined
Aug 27, 2002
Messages
131
I have an Excel file that looks somethng like below
Rubbish text = can be any text but we want to get rid of it
Message text = This is the actual word in the cell. We don't need this row but it is always before the row we need to keep.
Important text = can be any text but these are the only rows we want to keep. The only way to identify this row is when the row before it has the word "Message text"

Is it possible to do this with a macro?

Column A

Rubbish text
Rubbish text
Rubbish text
Message text
Important text
Rubbish text
Rubbish text
Rubbish text
Rubbish text
Rubbish text
Message text
Important text
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Code:
Sub ImportantMessages()

    Dim i As Long
    
    For i = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1
        If Not Cells(i - 1, "A") Like "Message text*" Then
            Cells(i, 1).EntireRow.Delete
        End If
    Next
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,424
Members
452,914
Latest member
echoix

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