Consulting

Results 1 to 4 of 4

Thread: Hiding or unhiding columns based on result of calculated field

  1. #1

    Hiding or unhiding columns based on result of calculated field

    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

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If Q4 has a calculation in it, it should still work just the same, it will still pick up the cell value.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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

    [vba]

    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
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    XLD,
    Oops. I see I copied my old code instead of your old code. Thanks for the update. It works perfectly!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •