PDA

View Full Version : Hiding or unhiding columns based on result of calculated field



Raddrummer
11-10-2008, 11:45 AM
Hi Everyone,

A week or so ago I posted the following question: "I want to have Excel hide or unhide columns L:O based on the what integer the user puts into cell Q4." XLD was gracious to send me the event code shown below.

A recent revision has changed cell Q4 from a user input value to a calculated one. So my question is now: How can I modify the code below to work based on the result of the calcuation?

Thanks.

Current VBA Code:
Sub ShowHide()
If (Range("Q4") >= 0.75) And (Range("Q4") < 1) Then
Columns("l:o").Select
Selection.EntireColumn.Hidden = False
Else: Columns("l:o").Select
Selection.EntireColumn.Hidden = True
End If
End Sub

Bob Phillips
11-10-2008, 12:05 PM
If Q4 has a calculation in it, it should still work just the same, it will still pick up the cell value.

Bob Phillips
11-10-2008, 12:12 PM
I didn't think that code looked like mine, so I looked back on the previous thread, and I think I see what you are meaning now.

Try this, still event code, still in the worksheet code module



Private Sub Worksheet_Calculate()
Const WS_RANGE As String = "Q4" '<== change to suit

On Error GoTo ws_exit
Application.EnableEvents = False

With Me.Range(WS_RANGE)
Me.Columns("L:O").Hidden = .Value < 0.75 Or .Value >= 1
End With

ws_exit:
Application.EnableEvents = True
End Sub

Raddrummer
11-10-2008, 12:46 PM
XLD,
Oops. I see I copied my old code instead of your old code. Thanks for the update. It works perfectly!!:bow: :bow: