List of "find what" delete column

psrs0810

Well-known Member
Joined
Apr 14, 2009
Messages
1,109
I have 27 different titles I am finding then deleting using a long list of:

Cells.Find(What:="C1S", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.EntireColumn.Select
Selection.Delete

How can shorted the list?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Perhaps something like
Code:
Dim foundCell as Range
Dim oneSearchTerm as Variant

For Each oneSearchTerm in Array("C1S", "C2S", "Bxf")
    Do
        Set foundCell = Cells.Find(What:=oneSearchTerm, After:=ActiveCell, _
            LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If foundCell Is Nothing then
            foundCell.EntireColumn.Delete
        End If
    Loop Until (foundCell Is Nothing)
Next oneSearchTerm
 
Upvote 0
I was testing the wrong condition for deletion.
Code:
Sub test()
Dim foundCell As Range
Dim oneSearchTerm As Variant

For Each oneSearchTerm In Array("dog", "C2S", "goat")
    Do
        Set foundCell = Cells.Find(What:=oneSearchTerm, After:=ActiveCell, _
            LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
       [COLOR="Red"] If Not (foundCell Is Nothing) Then[/COLOR]
            foundCell.EntireColumn.Delete
        End If
    Loop Until (foundCell Is Nothing)
Next oneSearchTerm
End Sub
 
Upvote 0
that's better.

Hey, what can I add so that it only looks a column C and everything to the right?

When I ran the maco it must have found another cell that I need to keep (which is in column B)
 
Upvote 0
Code:
Sub test()
Dim foundCell As Range
Dim oneSearchTerm As Variant

For Each oneSearchTerm In Array("dog", "C2S", "goat")
    Do
        With ActiveSheet
            Set foundCell = Range(.Cells(3, 1), .Cells(.Rows.Count, .Columns.Count)).Find(What:=oneSearchTerm, After:=.Cells(3,1), _
                LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
            If Not (foundCell Is Nothing) Then
                foundCell.EntireColumn.Delete
            End If
        End With
    Loop Until (foundCell Is Nothing)
Next oneSearchTerm
End Sub
 
Upvote 0
I see that you added in (.Cells(3, 1 to look at column C, but it still deleted the info in column B.
 
Upvote 0
Oh, it would help if I got rows and columns in the right place.

Rich (BB code):
Sub test()
Dim foundCell As Range
Dim oneSearchTerm As Variant

For Each oneSearchTerm In Array("dog", "C2S", "goat")
    Do
        With ActiveSheet
            Set foundCell = Range(.Cells(1, 3), .Cells(.Rows.Count, .Columns.Count)).Find(What:=oneSearchTerm, _
                After:=.Cells(1, 3), _
                LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
            If Not (foundCell Is Nothing) Then
                foundCell.EntireColumn.Delete
            End If
        End With
    Loop Until (foundCell Is Nothing)
Next oneSearchTerm
End Sub
 
Upvote 0
there we go!!!

I should have picked up that the rows/columns were mixed up.

thanks for your help
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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