PDA

View Full Version : Solved: calling a macro based on cell value each time it changes



Raddrummer
10-29-2008, 02:59 PM
Hi everyone,

I want to have Excel hide or unhide columns L:O based on the what integer the user puts into cell Q4. I have the macro working correctly but need help calling it each time the user changes the value in cell Q4.

Here is the code I have so far:


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

All help is greatly appreciated.

Bob Phillips
10-29-2008, 03:04 PM
Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "Q4" '<== change to suit

On Error GoTo ws_exit
Application.EnableEvents = False

If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
Me.Columns("L:O").Hidden = .Value < 0.75 Or .Value >= 1
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub


This is worksheet event code, which means that it needs to be
placed in the appropriate worksheet code module, not a standard
code module. To do this, right-click on the sheet tab, select
the View Code option from the menu, and paste the code in.

Raddrummer
10-29-2008, 03:11 PM
XLD you rock!!!:thumb I would have never figured that out.