+ Reply to Thread
Results 1 to 21 of 21

Help hiding rows based on drop down selection

Hybrid View

  1. #1
    Registered User
    Join Date
    06-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    15

    Help hiding rows based on drop down selection

    First time poster here so I hope I do this correctly!

    Using Excel 2010

    I am creating a form that will display different rows if certain selections are made. For example; cells B70-B72 are a drop down list with two options, '-' or 'sqrt'. If none equal the 'sqrt' symbol then I want to hide row 56. If one does, then row 56 will be displayed.

    If this is required, the drop-down list is selecting from Sheet:'List' where D7 is 'sqrt' and D8 is '-'

    There will be quite a few of these dependencies so ideally, I would be able to have many instances of the code and I could simply edit which cells are the drop down, and which cells would be shown or hidden. So for example, I could run the same code but change the drop down list to be cell B27 and then rows 36,67, and 45 would be hidden or shown.


    Any help at all would be fantastic.
    Thanks!
    Last edited by J-Fly; 06-25-2012 at 05:07 PM.

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Help hiding rows based on drop down selection

    To best describe or illustrate your problem you would be better off attaching a dummy workbook. The workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    To attach a file to your post, you need to be using the main 'New Post' or 'New Thread' page and not 'Quick Reply'.
    To use the main 'New Post' page, click the 'Post Reply' button in the relevant thread.

    On this page, below the message box, you will find a button labelled 'Manage Attachments'.
    Clicking this button will open a new window for uploading attachments.

    You can upload an attachment either from your computer or from another URL by using the appropriate box on this page.
    Alternatively you can click the Attachment Icon to open this page.

    To upload a file from your computer, click the 'Browse' button and locate the file.

    To upload a file from another URL, enter the full URL for the file in the second box on this page.
    Once you have completed one of the boxes, click 'Upload'.

    Once the upload is completed the file name will appear below the input boxes in this window.
    You can then close the window to return to the new post screen.
    HTH
    Regards, Jeff

  3. #3
    Registered User
    Join Date
    06-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    15

    Lightbulb Re: Help hiding rows based on drop down selection

    This is the dummy workbook I have created. It has two examples of hiding and showing rows in it.
    If anything else is needed I will deliver it!

    Thanks
    Attached Files Attached Files

  4. #4
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Help hiding rows based on drop down selection

    Thanks for the workbook example.

    First off, merged cells are evil. Please get rid of them if you can especially when you are going to introduce VBA. Makes life much harder.

    Now, I see two bits of instructions on this sheet. In row 4 I see, "IF check then show rows 18-30 IF not then hide"

    In row 26 I see, "only display if B15 is checked".

    Are these the only two requirements you have right now?

  5. #5
    Registered User
    Join Date
    06-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Help hiding rows based on drop down selection

    For now, yes. I feel that once I have the code for those two commands I would be able to shape them the way I need. Those are the only two types of commands that could pop up in the future.


    I have attached the workbook without the merged cells.
    Attached Files Attached Files

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Help hiding rows based on drop down selection

    See if you can adadt this.

    To unhide the row you can change the TRUE to FALSE
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    06-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Help hiding rows based on drop down selection

    For whatever reason, I am not able to get this to work. When I change it from True to False nothing happens and I get a dead cell.
    I have a screen capture here for what I am experiencing.
    Attached Images Attached Images

  8. #8
    Registered User
    Join Date
    06-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Help hiding rows based on drop down selection

    When I open the worksheet I get:

    Run-Time error '1004':
    Activate method of Worksheet class failed

  9. #9
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Help hiding rows based on drop down selection

    I'm sorry I think I left something out.

    The True and False was referring to the code. Right click on the sheet tab >> view code

    One the cell where you have the check, the code is looking for an 'a'. Go to the cell where you want the check mark and change the font to Marlett. When you enter an 'a' the cell will turn into a simulated check box.

    Did you use the file I attach? You said you removed all the merged cell but there were some left behind which I removed.

    Also, instead of merged cell take a look at center across selection which gives the same look.

  10. #10
    Registered User
    Join Date
    06-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Help hiding rows based on drop down selection

    I have the excel file you have attached and that is what I am using.
    I am trying to hide cells 18 to 33 by changing B3 to blank however when I do that, nothing changes. Is there a false command I am not seeing?
    Additionally, when I try to put the 'a' back in, I am getting the error: The value you entered is not valid. A user has restricted values that can be entered into this cell.

    And thanks for your suggestion! You answered a question I was thinking before I even had a chance to ask it!!


    Thank you for all your help!

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Help hiding rows based on drop down selection

    Try this, cell B3 and B13
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    06-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Help hiding rows based on drop down selection

    Amazing. Absolutely amazing!

    I can now see the mechanics in action. My only concern is that the drop down list only has the option for 'a'.
    So, to set it to false I need to right click-select from drop down list-and then choose the sqrt symbol.
    How did you create this drop down list with 'a'?

  13. #13
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Help hiding rows based on drop down selection

    Here is how you create data validation...

    Select cell >> Data tab >> Data Tools >> Data Validation >> Allow: List >> Source: a,q

    Set cell to Marlett font. Not sure if this is the symbol you want, but when using the marlett font (or wingdings or any other font) you are limited to what is there.

    Whatever you use, it also needs to be updated in the code. Right now the code works off of 'a' (hides rows) or nothing (unhides rows).

    Here is another alternative to consider

  14. #14
    Registered User
    Join Date
    06-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Help hiding rows based on drop down selection

    Thank you so much for your help. This is amazing and I should be set! (hopefully for good)

    You are awesome

  15. #15
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Help hiding rows based on drop down selection

    You are most welcome and glad you have a working model. Thanks for the feedback and don't forget to mark your thread as sloved

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  16. #16
    Registered User
    Join Date
    11-23-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Help hiding rows based on drop down selection

    Hi,

    Thanks for the above code. It helped me as well.

    It is working fine but when I protect my sheet it does not work and throws an error which says " Run -time error 1004: Unable to set the hidden property of the Range class".

    Please help on this..

    Thnaks..

  17. #17
    Registered User
    Join Date
    06-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Help hiding rows based on drop down selection

    One thing that didn't come to mind originally.
    Going back to the dummy workbook, how could I add a piece of code that would allow two changes to come from one case.

    IE:
    If Target.Value = "a" Then
                Rows("18:30").Hidden = True
            Else
                Rows("18:30").Hidden = False
            End If
    And, it would also hide Row 12.

  18. #18
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Help hiding rows based on drop down selection

    Try

            If Target.Value = "a" Then
                Rows("12").Hidden = True
                Rows("18:30").Hidden = True
            Else
                Rows("12").Hidden = False
                Rows("18:30").Hidden = False
            End If
    Last edited by jeffreybrown; 06-25-2012 at 06:21 PM. Reason: Update a true to false

  19. #19
    Registered User
    Join Date
    06-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Help hiding rows based on drop down selection

    Works like a charm.

    Now I have one final question.

    I have re uploaded the dummy workbook so you can see what I mean more clearly.

    This code
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim Rng As Range
        Set Rng = Target.Parent.Range("B3, B13")
        If Target.Count > 1 Then Exit Sub
        If Intersect(Target, Rng) Is Nothing Then Exit Sub
        Select Case Target.Address
        Case "$B$3"
            If Target.Value = "a" Then
                Rows("18:30").Hidden = True
            Else
                Rows("18:30").Hidden = False
            End If
        Case "$B$13"
            If Target.Value = "a" Then
                Rows("27").Hidden = True
            Else
                Rows("27").Hidden = False
            End If
        End Select
    End Sub
    Works very well, but how can I alter it so the Case$B$3 has two possible If target.values. IE) Option 1 do this, and for option 2 do this?
    Attached Files Attached Files

  20. #20
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Help hiding rows based on drop down selection

    See how this does for you...

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim Rng As Range
        Set Rng = Target.Parent.Range("B3, B13")
        If Target.Count > 1 Then Exit Sub
        If Intersect(Target, Rng) Is Nothing Then Exit Sub
        Select Case Target.Address
        Case "$B$3"
            If Target.Value = "Choice 1" Then
                Rows("17:22").Hidden = True
                Rows("23:26").Hidden = False
            ElseIf Target.Value = "Choice 2" Then
                Rows("23:26").Hidden = True
                Rows("17:22").Hidden = False
            Else
                Rows("17:26").Hidden = False
            End If
        Case "$B$13"
            If Target.Value = "a" Then
                Rows("27").Hidden = True
            Else
                Rows("27").Hidden = False
            End If
        End Select
    End Sub

  21. #21
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Help hiding rows based on drop down selection

    You will need to add the sheet unprotect and protect code lines to the main code. So unprotect before the actions, perform the actions then protect.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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