VBA to hide columns where cell does not match another

apinke1604

New Member
Joined
Dec 14, 2015
Messages
4
Hi,

I have been trying to find an answer to this but can't find anything specific or that I can adapt to fit so hopefully someone will be able to help!

My data looks like this:

B C D E etc...
1
2 AP
3 AP KH TC
4 x x x
5 x x x
6 x x x
etc...

with B2 being a dropdown list that includes AP,KH,TC etc and TEAM. I would like all columns to show when TEAM is selected, and only the columns that contain the matching initials to show otherwise i.e. if KH is selected in B2 then only column D would be visible and the others would be hidden. Is there a way I can do this with VBA? Thanks in advance
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Please copy to sheet module.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Dim C
    If Intersect(Target, Range("B2")) Is Nothing Then
        Exit Sub
    Else
        Cells.EntireColumn.Hidden = False
        Set rng = Range(Range("C3"), Cells(3, Columns.Count).End(xlToLeft))
        If Range("B2").Value = "TEAM" Then
            Cells.EntireColumn.Hidden = False
        Else
            For Each C In rng
                If C.Value <> Range("B2").Value Then
                    Columns(C.Column).EntireColumn.Hidden = True
                End If
            Next
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,761
Messages
6,132,570
Members
449,736
Latest member
anthx

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