Find and delete header columns without a certain word.

ittan

New Member
Joined
Oct 23, 2013
Messages
37
Hi,
I need to:
1. Find a column containing the word 'target' in header. (from the range P1 to QI1 )
2. Delete all other column without 'target' in its header.

Code:
Sub Cleanup()
    Dim rng As Range
    With ActiveSheet.Range("P1:QI1")
        Set rng = ActiveSheet.Range("P1:QI1").Find(What:="target", _
            LookAt:=xlPart, MatchCase:=False)
        Do While Not rng Is Nothing
            rng.EntireColumn.Delete
            Set rng = .FindNext
        Loop
    End With
End Sub

the code above is deleting all the column with the word 'target'.
thanks in advance.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try

Code:
Sub cleanup()
Dim j As Long
For j = 451 To 16 Step -1
    If Not LCase(Cells(1, j).Value) Like "*target*" Then Columns(j).Delete
Next j
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,987
Members
452,373
Latest member
TimReeks

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