+ Reply to Thread
Results 1 to 6 of 6

How VB code can unhide/hide rows based on combobox selection?

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    12-22-2010
    Location
    Texas
    MS-Off Ver
    Excel 2003 & Excel/Access 2007
    Posts
    438

    How VB code can unhide/hide rows based on combobox selection?

    Good morning… I have the code you see below that works but I would like to add to it because the drop down box has multiple choices.
    What I have works… I just don’t know how to add to it. This is what I would like for it to do:

    If “RE” or “Both Non-RE & RE” are selected I would like rows 88:89 & 99:102 to be unhidden

    If “NON-RE” is selected I would like for row 99 to be unhidden

    I am trying to learn… just a slow process.

    Formula: copy to clipboard
    'Collateral type'
    Private Sub ComboBox5_Change()

    Dim Rng As Range

    Rows("91:102").Hidden = 1

    If ComboBox5.Value <> "" And ComboBox5.Value <> "Unsecured" Then
    Set Rng = Range("A91:J102")
    Rng.Resize(1 * ComboBox5.Value + ComboBox5.Value - 1).EntireRow.Hidden = 0
    End If

    End Sub
    Attached Files Attached Files
    Last edited by jgomez; 09-04-2012 at 01:24 PM.

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: How to add more VB code to unhide/hide rows based on combobox selection?

    Whenever you have several possible outcomes to test, you should consider using the Select Case branching and control structure.
    Select Case ComboBox5.Value
         Case Is = ""
              'What to do when blank is chosen
         Case Is = "Unsecured"
              'What to do when Unsecured is chosen
         Case Is = "RE"
              'What to do when RE is chosen
         Case Is = "Both Non-RE & RE"
              'What to do when Both Non-RE & RE is chosen
         Case Else
              'What to do when the user makes any other choice
    End Select
    Insert your row hide/unhide procedure after the commented lines which begin with 'What to do when...
    Last edited by tlafferty; 08-23-2012 at 01:27 PM. Reason: Improper Code Tags
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Valued Forum Contributor
    Join Date
    12-22-2010
    Location
    Texas
    MS-Off Ver
    Excel 2003 & Excel/Access 2007
    Posts
    438

    Re: How to add more VB code to unhide/hide rows based on combobox selection?

    This is what I did:

    Formula: copy to clipboard
    Private Sub ComboBox5_Change()

    Select Case ComboBox5.Value

    Dim Rng As Range

    Case Is = ""
    'What to do when blank is chosen
    Rows("91:103").EntireRow.Hidden = True
    Case Is = "Unsecured"
    'What to do when Unsecured is chosen
    Rows("91:103").EntireRow.Hidden = True
    Case Is = "RE"
    'What to do when RE is chosen
    Rows ("88:89") And Rows("99:102").EntireRow.Hidden = False
    Case Is = "Both NON-RE & RE"
    'What to do when Both NON-RE & RE is chosen
    Rows ("88:89") And Rows("99:102").EntireRow.Hidden = False

    End Select

    End Sub


    It works for the "" & when Unsecured are selected but not for the last two. I think because I want to unhide certain rows & not others... Not sure how to write that part.

  4. #4
    Valued Forum Contributor
    Join Date
    12-22-2010
    Location
    Texas
    MS-Off Ver
    Excel 2003 & Excel/Access 2007
    Posts
    438

    Re: How to add more VB code to unhide/hide rows based on combobox selection?

    Anyone have a clue? Google is no help here... but tha'ts prob. because I don't know what to look for.

  5. #5
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: How VB code can unhide/hide rows based on combobox selection?

    For rows to unhide:
    Rows ("88:89").EntireRow.Hidden = False
    Rows("99:102").EntireRow.Hidden = False

  6. #6
    Valued Forum Contributor
    Join Date
    12-22-2010
    Location
    Texas
    MS-Off Ver
    Excel 2003 & Excel/Access 2007
    Posts
    438

    Re: How VB code can unhide/hide rows based on combobox selection?

    Thank you tlafferty... i actually figured this out at home... just don't have an internet connection there & I've been overwhelmed with work that I didn't get to mark this thread solved. Thanks again! Learned a bit...

+ 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