Delete Entire column based on word search

BradleyS

Active Member
Joined
Oct 28, 2006
Messages
301
I'm not very good at VBA and am struggling to work out a way to search across the top row column headers and delete the entire column if the following words do not exist in the relevant text description of that column.

The columns headers will have different text, but just in case it helps I only want to search the text held within the brackets.
The headers are written like this: A1 = "Example text (H12345)", B1 = "Example text (H12678)", C1 = "Example text (Y10678)"

My code is below but doesn't work. I only want to keep the column if: any text starts "H84*" or "Y01*" within the brackets. If there is no text like this (in the brackets) it needs to delete the column.

If searching text within brackets makes it over complicated, I'd just be happy with a text search of the cell contents.

<code>
Set MR = Range("A1:AG1")

For Each cell In MR
If cell.Value <> "H84*" And cell.Value <> "Y01*" Then cell.EntireColumn.Delete
Next
</code>
 

Some videos you may like

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,203
Office Version
  1. 365
Platform
  1. Windows
Try it like
Code:
Dim rng As Range

For Each cell In MR
If Not cell.Value Like "*(H84*" And Not cell.Value Like "*(Y01*" Then
   If rng Is Nothing Then Set rng = cell Else Set rng = Union(rng, cell)
End If
Next
If Not rng Is Nothing Then rng.EntireColumn.Delete
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,203
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,123,134
Messages
5,599,914
Members
414,348
Latest member
KloppyM

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
Top