Hide Columns Based on a Cell Value that DO NOT contain one of multiple values or a wildcard serach...

hallba

New Member
Joined
Jan 9, 2014
Messages
1
Hello,

I’m a macro/VBA rookie and I’m trying to build a macro that hides or shows columns that don’t contain multiple values or partial text strings. I’ve found and modified code that will hide or show all columns that don’t contain one value, but I’m having trouble with using a wildcard or multiple criteria.
Here’s the code I’m trying to use. The “Or” command seems to be the problem.

Code:
Sub PRG_SHOW()
Columns("Q:AIG").Select
    Selection.EntireColumn.Hidden = False
    Dim rng As Range
    Dim cl As Range
    Application.ScreenUpdating = False
    Set rng = [R2:AIG2]
    For Each cl In rng
        If IsError(cl) Then
            cl.EntireColumn.Hidden = cl = CVErr(xlErrNA)
        Else
            cl.EntireColumn.Hidden = cl <> "S-PRG" Or "C-PRG"
            End If
    Next
    Application.ScreenUpdating = True
    Range("M1").Select
End Sub

If I delete Or “C-PRG”, then the code works fine and shows only columns with the heading “S-PRG.”

I also tried using a wildcard but these did not work:
cl.EntireColumn.Hidden = cl <> "*PRG"
cl.EntireColumn.Hidden = cl <> "*PRG*"

In plain ‘ole English, I need the macro to hide all columns based on a cell value that don’t contain LABEL1, LABEL2, etc. (C-PRG and S-PRG in my code above.) I’d also need a macro that hides all columns based on a cell value that don’t contain a wildcard search, such as all columns whose cell reference does not contain *BEL* (I’m using BEL as an example since it is contain in the word LABEL.)
Can anyone help?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Forum statistics

Threads
1,203,069
Messages
6,053,352
Members
444,655
Latest member
didr

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