VBA - how to hide columns in various sheets according to criteria

davey4444

Board Regular
Joined
Nov 16, 2010
Messages
97
Hi all,

I have 3 worksheets with different values in row 5 (values can only be a number under 10) and I would like to cycle through the sheets and hide any columns in E:U where the value in the corresponding row 5 is not 0 or 1.
i.e if cell E5 = 0, do not hide and if cells F5 and G5 = 2 then hide them.

I have looked through various examples of hiding columns/rows according to specific criteria however nothing seems to exactly match what I need and I'm not of the level to manipulate the code to my needs.

Thanks in advance.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Code:
Sub hideSheets()



    For x = 1 To Worksheets.Count
    With Sheets(x)
        If .Range("E5") = 0 Then
            .Visible = True
        ElseIf .Range("F5") = 2 And .Range("G5") = 2 Then
            .Visible = False
        End If
    End With
    Next x




End Sub

This assumes that E5 take priority over F5=G5=2
 
Upvote 0
Hi,

Thanks for the reply but won't this hide the entire sheet? I just want to hide the columns which do not contain 0 or 1.

Thanks.
 
Upvote 0
Yup, you're right. I read your question wrong, sorry about that.

Code:
Sub hideSheets()



    For x = 1 To Worksheets.Count
    With Sheets(x)
        For y = 5 To 21
            If .Cells(5, y).value = 0 Or .Cells(5, y).value = 1 Then
                .Cells(5, y).EntireColumn.Hidden = False
            Else
                .Cells(5, y).EntireColumn.Hidden = True
            End If
        Next y
    End With
    Next x




End Sub
 
Upvote 0
You can also do this with one line of code (albeit a long one)...

Code:
Sub HideColumns()
  Range(Replace(Application.Trim(Join(Evaluate("IF(E5:U5>1,ADDRESS(1,COLUMN(E5:U5),4)&"":""&ADDRESS(1,COLUMN(E5:U5),4),"""")"))), " ", ",")).EntireColumn.Hidden = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,858
Messages
6,121,960
Members
449,057
Latest member
FreeCricketId

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