Automaticaly Hiding Columns

Cjbnash

New Member
Joined
Jan 23, 2005
Messages
8
Hi there,

I am using the below bit of code to hide columns based on a value. However it wont work and cant figure it out.

any ideas
Thanks
Chris

Sub Show_Team_1()

Application.ScreenUpdating = False
Sheets("Master Schedule").Visible = True
Sheets("Master Schedule").Select

Dim c As Range

Range("h9:bh9").EntireColumn.Hidden = False

For Each c In Range("h9:bh9")
If c = "1" Then
Else
Columns(c.Columns).EntireColumn.Hidden = True
End If
Next c
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Welcome to the Board Chris.

You've already defined c as a Range and are looping through each cell in the range, so you can simply use
Code:
c.EntireColumn.Hidden = True
so your code would become
Code:
Sub Show_Team_1()

Application.ScreenUpdating = False
Sheets("Master Schedule").Visible = True
Sheets("Master Schedule").Select

Dim c As Range

Range("h9:bh9").EntireColumn.Hidden = False

For Each c In Range("h9:bh9")
    If c = "1" Then
        c.EntireColumn.Hidden = True
    End If
Next c
Application.ScreenUpdating = True

End Sub
HTH

Regards
 
Upvote 0
Chris

What exactly do you mean it won't work?

Is it causing errors or not doing what you expect?

How are you running the code? Which sheet is active when you run it?

Code:
Sub Show_Team_1() 
Dim rng As Range
Dim c As Range 

     Application.ScreenUpdating = False

     Set rng = Sheets("Master Schedule").Range("h9:bh9")
     
     rng.EntireColumn.Hidden = False

     For Each c In rng
          c.EntireColumn.Hidden = c="1"
     Next c 

     Application.ScreenUpdating = True 

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,220
Members
448,876
Latest member
Solitario

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