Hide Columns

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Hide Columns

Post by adam »

Hi anyone.

The following code hides columns with the text value in the code. How could I make the code to hide all the columns that do not match the value in cell “G1”.

Suppose I write “January" in “G1”. I want all the columns that do not contain the Month "January" to be hidden.

Code: Select all

Sub HideColumns()
Application.ScreenUpdating = False
Dim c As Range
    For Each c In Range("A2:E2")
        c.EntireColumn.Hidden = (c.Value = "January")
    Next c
Application.ScreenUpdating = True
End Sub
Any help on this would be kindly appreciated.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78671
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Hide Columns

Post by HansV »

c.EntireColumn.Hidden = (c.Value <> Range("G1"))
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Hide Columns

Post by adam »

Thanks for the help Hans. It works fine now.
Best Regards,
Adam