Delete Entire column based on word search

BradleyS

Active Member
Joined
Oct 28, 2006
Messages
351
Office Version
  1. 2010
Platform
  1. Windows
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>
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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
 
Upvote 0
Solution
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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