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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Do you want the search for those two strings to be case sensitive?
 
Upvote 0
Do you want the search for those two strings to be case sensitive?
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.
 
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,215,606
Messages
6,125,811
Members
449,262
Latest member
hideto94

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