Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Posts: 29
Default Hide Co9lumns based on cell value

Hi,

I have some vba to hide some columns in one sheet based on a cell value in another sheet:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Sheets("Start").Range("U4").Value = "OFF" Then
Columns("P:AB").EntireColumn.Hidden = True
Else
Columns("P:AB").EntireColumn.Hidden = False
End If
End Sub

As you can see I have placed this code in the Worksheet_SelectionChange. This works fine, however it looks for that cell value everytime the user moves around the sheet. What I'm after is to only look at the value once when the workbook opens (as it will not change from then on).

I have tried placing the code in both Private Sub's Worksheet_Change & Worksheet_Calculate, but the existing code does not work in either of these.

Any advice would be appreciated.

Cheers.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Hide Co9lumns based on cell value

Hi Gary,

Am Mon, 21 Jan 2013 02:46:05 +0000 schrieb garygoodguy:

I have some vba to hide some columns in one sheet based on a cell value
in another sheet:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Sheets("Start").Range("U4").Value = "OFF" Then
Columns("P:AB").EntireColumn.Hidden = True
Else
Columns("P:AB").EntireColumn.Hidden = False
End If
End Sub


in which sheet (name) you want to hide the columns?


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default Hide Co9lumns based on cell value

Gary,

Place this code in the ThisWorkbook module and change "MySheet" to the sheet you wish to hide columns on.

Ben


Private Sub Workbook_Open()

Sheets("MySheet").Columns("P:AB").EntireColumn.Hid den = _
(Sheets("Start").Range("U4").Value = "OFF")

End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default Hide Co9lumns based on cell value

Gary,

One other thought: you could enter the code on the "Start" sheet's code module under the Deactivate event. That way the users can change cell U4 of the "Start" sheet and see the change right away, but you won't have the event firing every time a cell is selected on other sheets. Here's the modified code:

Private Sub Worksheet_Deactivate()

Sheets("MySheet").Columns("P:AB").EntireColumn.Hid den = _
(Sheets("Start").Range("U4").Value = "OFF")

End Sub
  #5   Report Post  
Junior Member
 
Posts: 29
Default

Thanks Ben.

But this code gave me a syntax error.



Quote:
Originally Posted by Ben McClave View Post
Gary,

Place this code in the ThisWorkbook module and change "MySheet" to the sheet you wish to hide columns on.

Ben


Private Sub Workbook_Open()

Sheets("MySheet").Columns("P:AB").EntireColumn.Hid den = _
(Sheets("Start").Range("U4").Value = "OFF")

End Sub


  #6   Report Post  
Junior Member
 
Posts: 29
Default

Claus,

There are multiple sheets (140 odd). An example is "1. Forecast".

Quote:
Originally Posted by Claus Busch View Post
Hi Gary,

Am Mon, 21 Jan 2013 02:46:05 +0000 schrieb garygoodguy:

I have some vba to hide some columns in one sheet based on a cell value
in another sheet:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Sheets("Start").Range("U4").Value = "OFF" Then
Columns("P:AB").EntireColumn.Hidden = True
Else
Columns("P:AB").EntireColumn.Hidden = False
End If
End Sub


in which sheet (name) you want to hide the columns?


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #7   Report Post  
Junior Member
 
Posts: 29
Default

Quote:
Originally Posted by garygoodguy View Post
Hi,

I have some vba to hide some columns in one sheet based on a cell value in another sheet:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Sheets("Start").Range("U4").Value = "OFF" Then
Columns("P:AB").EntireColumn.Hidden = True
Else
Columns("P:AB").EntireColumn.Hidden = False
End If
End Sub

As you can see I have placed this code in the Worksheet_SelectionChange. This works fine, however it looks for that cell value everytime the user moves around the sheet. What I'm after is to only look at the value once when the workbook opens (as it will not change from then on).

I have tried placing the code in both Private Sub's Worksheet_Change & Worksheet_Calculate, but the existing code does not work in either of these.

Any advice would be appreciated.

Cheers.

So I got this to work. Placed this code in workbook_open and just added each sheet as necessary:


If Sheets("Start").Range("U4").Value = "OFF" Then
Sheets("MySheet").Columns("P:AB").EntireColumn.Hid den = True
Else
Sheets("MySheet").Columns("P:AB").EntireColumn.Hid den = False
End If

Thanks for the help.
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 277
Default Hide Co9lumns based on cell value

On Tue, 22 Jan 2013 07:02:08 +0000, garygoodguy
wrote:



So I got this to work. Placed this code in workbook_open and just added
each sheet as necessary:


If Sheets("Start").Range("U4").Value = "OFF" Then
Sheets("MySheet").Columns("P:AB").EntireColumn.Hi dden = True
Else
Sheets("MySheet").Columns("P:AB").EntireColumn.Hi dden = False
End If

Thanks for the help.



I assume this would also work with rows?

I can make a version of this for my leap year handling, instead of
giving the user a pair of hide unhide buttons.
  #9   Report Post  
Junior Member
 
Posts: 29
Default

Not an expert - but don't see why it wouldn't also work with rows.

Quote:
Originally Posted by CellShocked View Post
On Tue, 22 Jan 2013 07:02:08 +0000, garygoodguy
wrote:



So I got this to work. Placed this code in workbook_open and just added
each sheet as necessary:


If Sheets("Start").Range("U4").Value = "OFF" Then
Sheets("MySheet").Columns("P:AB").EntireColumn.Hi dden = True
Else
Sheets("MySheet").Columns("P:AB").EntireColumn.Hi dden = False
End If

Thanks for the help.



I assume this would also work with rows?

I can make a version of this for my leap year handling, instead of
giving the user a pair of hide unhide buttons.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
hide row based on cell value jat Excel Worksheet Functions 2 February 19th 10 09:05 PM
Hide row based on cell value Munchkin Excel Worksheet Functions 2 June 25th 09 02:21 AM
Hide Next row based on Cell Value VS182501 Excel Programming 3 May 20th 09 10:22 AM
Is there a way to HIDE a row based on a value of a cell ? Reddiance Excel Discussion (Misc queries) 4 January 26th 05 02:57 AM
Hide Row Based on cell value GaryF Excel Programming 3 April 27th 04 04:55 PM


All times are GMT +1. The time now is 09:02 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"