Macro to delete everything below the header depending on the specific text but don't delete the header

tropics123

Board Regular
Joined
May 11, 2016
Messages
85
Hi! Could someone help me tweak this macro? I want the macro to find specific texts in the header and (the text will appear multiple times) and delete everything in that column but don't delete the header. For example, I want the macro to look for "Mary Lee" and "Bob Smith" in the header and delete everything in the column below Mary Lee and Bob Smith's name but leave Mary Lee and Bob Smith's name in the header.

This is the before table:
[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]Mary Lee[/TD]
[TD]Bob Smith[/TD]
[TD]Luke Skywalker[/TD]
[TD]Mary Lee[/TD]
[TD]Bob Smith[/TD]
[TD]Mary Lee[/TD]
[TD]Joe Watson[/TD]
[/TR]
[TR]
[TD]$450[/TD]
[TD]$2000[/TD]
[TD]$90[/TD]
[TD]$78[/TD]
[TD]$88[/TD]
[TD]$67[/TD]
[TD]$23[/TD]
[/TR]
[TR]
[TD]$57[/TD]
[TD]$709[/TD]
[TD]$19[/TD]
[TD]$59[/TD]
[TD]$39[/TD]
[TD]$78[/TD]
[TD]$587[/TD]
[/TR]
[TR]
[TD]$98[/TD]
[TD]$39[/TD]
[TD]$35[/TD]
[TD]$56[/TD]
[TD]$28[/TD]
[TD]$55[/TD]
[TD]$6560[/TD]
[/TR]
</tbody>[/TABLE]

After:
[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]Mary Lee[/TD]
[TD]Bob Smith[/TD]
[TD]Luke Skywalker[/TD]
[TD]Mary Lee[/TD]
[TD]Bob Smith[/TD]
[TD]Mary Lee[/TD]
[TD]Joe Watson[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]$90[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]$23[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]$19[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]$587[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]$35[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]$6560

[/TD]
[/TR]
</tbody>[/TABLE]

Here's my macro but it's deleting everything in the column including the header. Thank you for your help!

Sub Delete()


' Delete cells below header with specific text but don't delete header
Sheets("Sheet3").Select
Dim A As Range



Do
Set A = Rows(1).Find(What:="Mary Lee", LookIn:=xlValues, lookat:=xlPart)
Set A = Rows(1).Find(What:="Bob Smith", LookIn:=xlValues, lookat:=xlPart)
If A Is Nothing Then Exit Do
A.EntireColumn.Delete
Loop
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Assumes headers are in row 1.
Code:
Sub RemoveAllBelowHeader()
'assumes all headers are in row 1
Dim tgtHdrs As Variant, Fnd As Range, fAdr As String
tgtHdrs = Array("Mary Lee", "Bob Smith") ' headers to remove data below the header,change to suit
Application.ScreenUpdating = False
For i = LBound(tgtHdrs) To UBound(tgtHdrs)
    Set Fnd = Rows(1).Find(what:=tgtHdrs(i), lookat:=xlWhole, MatchCase:=False)
    If Not Fnd Is Nothing Then
        fAdr = Fnd.Address
    End If
    Do
        Range(Fnd.Offset(1, 0), Cells(Rows.Count, Fnd.Column).End(xlUp)).ClearContents
        Set Fnd = Rows(1).FindNext(Fnd)
        If Fnd Is Nothing Then Exit Do
        If Fnd.Address = fAdr Then Exit Do
    Loop
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Another way.

Code:
Sub HDELETE()
Application.ScreenUpdating = False
Dim LR As Long: LR = Range("A" & Rows.Count).End(xlUp).Row
Dim r As Range: Set r = Range("A1").CurrentRegion.Resize(1)
Dim s As String: s = "Mary Lee;Bob Smith"
Dim tmp As Range

For Each cel In r
    If InStr(s, cel.Value) > 0 Then
        Set tmp = cel.Offset(1).Resize(LR - 1, 1)
        tmp.ClearContents
    End If
Next cel
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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