Macro to Delete rows containing specific text and one row above

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
i have written code to delete rows containing "Mersand" in Col A from row 2 onwards


I need this amended to delete the row above "Mersand" as well

it would be appreciated if someone would kindly amend my code

Code:
 Sub Del_Mersand()
With Sheets("Imported Data")
Dim LR As Long, i As Long
LR = .Range("A" & .Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
If .Range("A" & i).Value Like "*Mersand*" Then Range("A" & i).EntireRow.Delete

Next i
End With
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Changing this line:
VBA Code:
If .Range("A" & i).Value Like "*Mersand*" Then Range("A" & i).EntireRow.Delete
to this:
VBA Code:
If .Range("A" & i).Value Like "*Mersand*" Then .Rows(i-1 & ":" & i).Delete
 
Upvote 0
Solution
Thanks for the help Joe. Code works perfectly
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,240
Members
448,555
Latest member
RobertJones1986

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