Macro to delete Rows

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,601
Office Version
  1. 2021
Platform
  1. Windows
I have tried to wrire code to delete rows starting from one row above where txt " Detail L" appears to 9 rows below where "Detail L" , but cannot get the code to work properly

I have attached sample data. The rows to be deleted must be 56 to 65 in this example-see my code and sample data below

Your assistance will be most appreciated

Sub Del_Unwanted()
Finalrow = Cells(65536, 1).End(xlUp).Row
For i = Finalrow To 5 Step -1
If Cells(i, 1).Value = "Detail L" Then

Cells(i, 1).Offset(-1).Resize(10).EntireRow.Delete
End If
Next i
end Sub

East1101P.xls
ABCDEFG
55684PTS26/02/11FrontCounterInvoice841035757
56
57DetailLineAgeing:N00Current-37939.93P17939.93
58EAMonFeb2810:27:482011Pa
59=============================================================================
60
61Nominalaccount
62---------------------
63
64RefNoJrnDateNarrativeCtlTransamt.Ctlbalance
65---------------------------------------------
66685PTS26/02/11FrontCounterInvoice84104508.95508.95
Sheet1
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try

Code:
Sub Del_Unwanted()
Dim Found As Range
Set Found = Columns("A").Find(what:="Detail L", LookIn:=xlValues, lookat:=xlWhole)
If Not Found Is Nothing Then Found.Resize(10).EntireRow.Delete
End Sub
 
Upvote 0
or
Code:
Sub Del_Unwanted()
  on error resume next
  Columns(1).Find("Detail L", ,xlValues, xlWhole).Resize(10).EntireRow.Delete
End Sub
 
Upvote 0
Hi Guys

Thanks for the help, much appreciated

Regards

Howard
 
Upvote 0
Try

Code:
Sub Del_Unwanted()
Dim Found As Range
Set Found = Columns("A").Find(what:="Detail L", LookIn:=xlValues, lookat:=xlWhole)
If Not Found Is Nothing Then Found.Resize(10).EntireRow.Delete
End Sub

Hi Peter

You helped me the other day with code to delete one row above where the word "detail" appeared and 10 rows below. This code deleted the coorect amount of rows where detail appears once, but when there are several page breaks where the word "detail" appears, the code does not delete these. It would be appreciated if you would amend your code to take this into account

Thanks

Howard
 
Upvote 0
Hi. Try this

Code:
Sub Del_Unwanted()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    If Range("A" & i).Value = "Detail L" Then Range("A" & i).Resize(10).EntireRow.Delete
Next i
End Sub
 
Upvote 0
Hi Peter

Thanks for the help, much appreciated
 
Upvote 0
Hi. Try this

Code:
Sub Del_Unwanted()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    If Range("A" & i).Value = "Detail L" Then Range("A" & i).Resize(10).EntireRow.Delete
Next i
End Sub

Hi Peter

When activating the macro, the code
"If Range("A" & i).Value = "Detail L" Then Range("A" & i).Resize(10).EntireRow.Delete" is highlighted

It would be appreciated if you would test and correct

Regards

Howard
 
Upvote 0
Hi. Perhaps you have error values in Column A. Try

Code:
Sub Del_Unwanted()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    If Not IsError(Range("A" & i)) Then
        If Range("A" & i).Value = "Detail L" Then Range("A" & i).Resize(10).EntireRow.Delete
    End If
Next i
End Sub
 
Upvote 0
Hi Peter

Thanks for the help. The macro runs perfectly

Have a great weekend
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,707
Members
452,939
Latest member
WCrawford

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