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

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Glaswegian

Well-known Member
Joined
Oct 14, 2003
Messages
1,487
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,318
Office Version
  1. 365
Platform
  1. Windows
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
 

Forum statistics

Threads
1,148,050
Messages
5,744,513
Members
423,881
Latest member
Nguyen Vu

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
Top