+ Reply to Thread
Results 1 to 9 of 9

Worksheet_Change event - hide/unhide columns based on multiple drop down list

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-23-2013
    Location
    Hungary
    MS-Off Ver
    Excel 2013
    Posts
    331

    Worksheet_Change event - hide/unhide columns based on multiple drop down list

    Hello,

    I would like to ask your support regarding a column hide/unhide issue. My target is to hide/unhide columns based on 4 drop down lists by Worksheet_Change event.

    This is the code what I use now, here I have only 1 drop down list (in cell C10) and if I select "No" then the code hide these columns, if I select "Yes" then the code unhide these columns. It is working well.

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        If Not Intersect(Target, [C10]) Is Nothing Then
            Columns.Hidden = 0
            Select Case Target
            Case Is = "Yes"
                Range("H:H,K:K,N:N,Q:Q,T:T,W:W").EntireColumn.Hidden = False
            Case Is = "No"
                Range("H:H,K:K,N:N,Q:Q,T:T,W:W").EntireColumn.Hidden = True
            End Select
        End If
        
    End Sub
    But how should be modified this code if I would like to add more drop down list, like:
    If the value in cell11 is "No" then these columns should be hided:
    Range("I:I,L:L,O:O,R:R,U:U,X:X").EntireColumn.Hidden = True
    If the value in cell11 is "Yes" then keep them unhided.

    or

    If the value in cell12 is "No" then these columns should be hided:
    Range("J:J,M:M,P:P,S:S,V:V,Y:Y").EntireColumn.Hidden = True
    If the value in cell12 is "Yes" then keep them unhided.

    or

    If the value in cell13 is "No" then these columns should be hided:
    Range("E:E,F:F,G:G").EntireColumn.Hidden = True
    If the value in cell13 is "Yes" then keep them unhided.

    Thank you in advance for your support!

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: Worksheet_Change event - hide/unhide columns based on multiple drop down list

    Try:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("C10:C13")) Is Nothing Then Exit Sub
        Columns.Hidden = 0
        If Target.Row = 10 Then
            Select Case Target.Value
                Case Is = "Yes"
                    Range("H:H,K:K,N:N,Q:Q,T:T,W:W").EntireColumn.Hidden = False
                Case Is = "No"
                    Range("H:H,K:K,N:N,Q:Q,T:T,W:W").EntireColumn.Hidden = True
            End Select
        ElseIf Target.Row = 11 Then
            Select Case Target.Value
                Case Is = "Yes"
                    Range("I:I,L:L,O:O,R:R,U:U,X:X").EntireColumn.Hidden = False
                Case Is = "No"
                    Range("I:I,L:L,O:O,R:R,U:U,X:X").EntireColumn.Hidden = True
            End Select
        ElseIf Target.Row = 12 Then
            Select Case Target.Value
                Case Is = "Yes"
                    Range("J:J,M:M,P:P,S:S,V:V,Y:Y").EntireColumn.Hidden = False
                Case Is = "No"
                    Range("J:J,M:M,P:P,S:S,V:V,Y:Y").EntireColumn.Hidden = True
            End Select
        ElseIf Target.Row = 13 Then
            Select Case Target.Value
                Case Is = "Yes"
                    Range("E:E,F:F,G:G").EntireColumn.Hidden = False
                Case Is = "No"
                    Range("E:E,F:F,G:G").EntireColumn.Hidden = True
            End Select
        End If
    End Sub
    Last edited by Mumps1; 03-08-2018 at 09:20 AM.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,888

    Re: Worksheet_Change event - hide/unhide columns based on multiple drop down list

    Another option would be
    Private Sub Worksheet_Change(ByVal Target As Range)
       If Target.CountLarge > 1 Then Exit Sub
       If Intersect(Target, Range("C10:C13")) Is Nothing Then Exit Sub
       Columns.Hidden = 0
       
       If Target.Row = 10 Then
          Range("H:H,K:K,N:N,Q:Q,T:T,W:W").EntireColumn.Hidden = Not Target.Value = "Yes"
       ElseIf Target.Row = 11 Then
          Range("I:I,L:L,O:O,R:R,U:U,X:X").EntireColumn.Hidden = Not Target.Value = "Yes"
       ElseIf Target.Row = 12 Then
          Range("J:J,M:M,P:P,S:S,V:V,Y:Y").EntireColumn.Hidden = Not Target.Value = "Yes"
       ElseIf Target.Row = 13 Then
          Range("E:E,F:F,G:G").EntireColumn.Hidden = Not Target.Value = "Yes"
       End If
    End Sub

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,888

    Re: Worksheet_Change event - hide/unhide columns based on multiple drop down list

    @mumps1
    Shouldn't that be
    If Target.Row= 10 Then
    etc?

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: Worksheet_Change event - hide/unhide columns based on multiple drop down list

    @Fluff: Thank you. I noticed that and changed it a few minutes ago.

  6. #6
    Forum Contributor
    Join Date
    12-23-2013
    Location
    Hungary
    MS-Off Ver
    Excel 2013
    Posts
    331

    Re: Worksheet_Change event - hide/unhide columns based on multiple drop down list

    Hi Mumps1, Fluff13,

    Thank you for your help!
    I've tried your codes and there is an open point which is not solved yet. If I select only one "No" then the code hide/unhide the columns as expected but if I select more "No" value then the hiding/unhiding is not working properly.

    Could you please see the attached sample file?
    Attached Files Attached Files

  7. #7
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,888

    Re: Worksheet_Change event - hide/unhide columns based on multiple drop down list

    If you remove this line
       Columns.Hidden = 0
    Does the code do what you want?

  8. #8
    Forum Contributor
    Join Date
    12-23-2013
    Location
    Hungary
    MS-Off Ver
    Excel 2013
    Posts
    331

    Re: Worksheet_Change event - hide/unhide columns based on multiple drop down list

    Yes, thank you very much!

  9. #9
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,888

    Re: Worksheet_Change event - hide/unhide columns based on multiple drop down list

    Glad we could help & thanks for the feedback

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Hide/Unhide Columns based on Drop-Down Selection
    By Khafez in forum Excel General
    Replies: 21
    Last Post: 09-14-2016, 12:13 PM
  2. Hide/Unhide Sheets based on drop down list
    By toci in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-01-2016, 08:06 AM
  3. Hide/Unhide cells based on drop-down list
    By Savvy25 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-08-2014, 02:36 AM
  4. Hide-Unhide rows on multiple worksheets based on value of a drop down list
    By clo2peter in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-10-2014, 08:32 AM
  5. [SOLVED] Hide/Unhide columns in multiple worksheets based on item chosen in a drop down list.
    By Gattaca2014 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-27-2014, 01:31 PM
  6. [SOLVED] hide or unhide rows based on text within a drop down list
    By souimet in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-17-2013, 01:32 AM
  7. Hide/unhide columns based on the drop down list value
    By vagif in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-16-2012, 02:13 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1