Macro needed to delete columns where one of two strings not present

jmclaren

New Member
Joined
Feb 18, 2016
Messages
5
Hi. I regularly receive a spreadsheet that has several hundred columns and a similar number of rows. From column 'D' onwards, the only columns I'm interested in are ones that contain either string "Outstanding" or "Required". Can anyone suggest a macro that would delete all columns after 'D' that don't contain one or other of these strings in at least one of the rows? Thanks.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Do you want the search for those two strings to be case sensitive?
 
Upvote 0
Hi Joe. The case will always be consistent, so if it helps to be precise, including case then that's fine - doesn't really matter though. Thanks.
Try this:
Code:
Sub DeleteIfNeither()
Const S1 As String = "Outstanding"
Const S2 As String = "Required"
Dim F As Range
Application.ScreenUpdating = False
For i = ActiveSheet.UsedRange.Columns.Count To 5 Step -1
    Set F = Columns(i).Find(S1, LookAt:=xlPart)
    If F Is Nothing Then
        Set F = Columns(i).Find(S2, LookAt:=xlPart)
        If F Is Nothing Then Columns(i).Delete
    End If
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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