Macro to delete all rows below Specific Text

athermian

New Member
Joined
Oct 31, 2020
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
  2. Mobile
  3. Web
Hi,

I am using Office 2016. I have got this macro from extended office website it works very nicely and deletes all rows before "Header Details".

How can this be modified for two more deletion scenarios?

1. All rows below Header Details
2. Delete all rows below Header Details starting from row 11

Any help or suggestions would be greatly appreciated.

Thanks,

Ather


VBA Code:
Sub DeleteRowsABove()
'UpdatebyExtendoffice20161109
    Dim fRg As Range
     
    Set fRg = Cells.Find(what:="Header Details", lookat:=xlWhole)
     
    If Not fRg Is Nothing Then
        If fRg.Row <> 1 Then
            Range("A1", fRg.Offset(-1)).EntireRow.Delete
        Else
            'MsgBox "Total Group is in the first row already", vbInformation, "Kutools for Excel"
        End If
    Else
       'MsgBox "Do not find Total Group", vbInformation, "Kutools for Excel"
    End If
     
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Here you go, two macro for the two conditions

VBA Code:
'1.All rows below Header Details
Sub DeleteRowsBelow()

    Dim fRg As Range
    Dim lr, i As Long
    lr = Cells(Rows.Count, "A").End(xlUp).Row 'Assumes rowdata existsin column A
    Set fRg = Cells.Find(what:="Header Details", lookat:=xlWhole)
    For i = lr To (fRg.Row + 1) Step -1
        Cells(i, 1).EntireRow.Delete
    Next i
     
End Sub

'2. Delete all rows below Header Details starting from row 11

Sub DeleteRowsBelow11()

    Dim fRg As Range
    Dim lr, i As Long
    lr = Cells(Rows.Count, "A").End(xlUp).Row 'Assumes rowdata existsin column A
    For i = lr To 11 Step -1
        Cells(i, 1).EntireRow.Delete
    Next i
     
End Sub
 
Upvote 0
@ManiacB,

Instead of looping, why not just delete the rows all at once?

In your first macro, replace the For..Next loop with this...
VBA Code:
Range(fRg, Cells(lr, "A")).EntireRow.Delete

In your second macro, replace the For..Next loop with this...
VBA Code:
Range("A11", Cells(lr, "A")).EntireRow.Delete
 
Upvote 0
Here you go, two macro for the two conditions

VBA Code:
'1.All rows below Header Details
Sub DeleteRowsBelow()

    Dim fRg As Range
    Dim lr, i As Long
    lr = Cells(Rows.Count, "A").End(xlUp).Row 'Assumes rowdata existsin column A
    Set fRg = Cells.Find(what:="Header Details", lookat:=xlWhole)
    For i = lr To (fRg.Row + 1) Step -1
        Cells(i, 1).EntireRow.Delete
    Next i
    
End Sub

'2. Delete all rows below Header Details starting from row 11

Sub DeleteRowsBelow11()

    Dim fRg As Range
    Dim lr, i As Long
    lr = Cells(Rows.Count, "A").End(xlUp).Row 'Assumes rowdata existsin column A
    For i = lr To 11 Step -1
        Cells(i, 1).EntireRow.Delete
    Next i
    
End Sub
Thank you ManiacB
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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