+ Reply to Thread
Results 1 to 13 of 13

Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

  1. #1
    Registered User
    Join Date
    11-22-2019
    Location
    Victoria, Canada
    MS-Off Ver
    Office 365
    Posts
    6

    Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    Hello Everyone,

    This is my first time posting here, so right off I'd like to say thank you for all of the great questions and answers here that have helped me so much already. I'm quite new to VBA, so this has been a lifeline for me.

    I am creating a template workbook with many tables which contain multiple dependent dropdown lists. These cascades appear to be working just fine in the tables via data validation using the INDIRECT function.

    My challenge has been to create this same functionality in the userforms.

    I have found several examples of code for similar situations to mine, but each one is just unique enough that I’m not able to recognize how to make it work for my particular use (yes, very new to VBA).

    The Context:

    To keep it simple, here are just some basic details for one of the tables…

    I’ll call this the MAIN table, and it has 3 columns: 1. “Type” 2. “Subtype” 3. “Item”

    1. The “Type” column has a drop-down list in it based on a single-column dynamic named range which is stored on a different sheet in the workbook.

    2. The “Subtype” column is dependent on the user’s drop-down list choice in the “Type” column and it draws on dynamic named ranges with names that exactly match the options in the “Type” column list. (These ranges are also housed on that other worksheet.)

    3. Similarly, the “Item” column is dependent on the user’s drop-down list choice in the “Subtype” column and draws on dynamic named ranges with names that exactly match the options in the “Subtype” column lists.

    So all named ranges are dynamic, single-column, and are stored on a sheet named “DropDownLists”. (The ranges are directly based on named tables.)

    The Userform:

    1. The “Type” field is a combo box that has already been coded to show all items in the named range called “rgeMAINItemType”. That seems to be working fine.
    Name: cboTypeMAIN

    2. The “Subtype” field is a combo box that SHOULD BE populated by the range whose name matches the selection the user enters into cboTypeMAIN.
    Name: cboSubtypeMAIN

    3. The “Item” field is a combo box that SHOULD BE populated by the range whose name matches the selection the user enters into cboSubtypeMAIN.
    Name: cboItemMAIN

    I won’t post any of the code I have been trying, in order to not confuse the matter here. Also, I realize that there might be potentials for errors that one should address within the coding that I am just not aware of...

    If anyone is able to assist me with a solution, it would be very greatly appreciated. (I have been working on this without success for a very long time!)

    (Simple workbook example attached.)

    Thank you very much!!
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    06-02-2012
    Location
    London, England
    MS-Off Ver
    365
    Posts
    397

    Re: Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    Hello and welcome

    For one way to do this (there are several), add the code below to the end of your userform module
    Please Login or Register  to view this content.
    Beth.
    Last edited by BanginMyHeadOnMyDesk; 12-21-2019 at 12:57 AM.

  3. #3
    Registered User
    Join Date
    11-22-2019
    Location
    Victoria, Canada
    MS-Off Ver
    Office 365
    Posts
    6

    Re: Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    Hi Beth,

    Thank you very much for your reply! Your code worked beautifully on that sample file that I had posted.

    On my actual file (which uses the same names), when I paste the code into the userform module the form no longer opens and "Compile Error: Variable not defined" pops up. It points to "ws" on the following line:

    Set ws = Sheets("DropDownLists")

    Since I used a similar line in the UserForm_initialize() procedure without problem, I tried changing the above line to match that, using the following 2 lines:

    Dim ws As Worksheet
    Set ws = Worksheets("DropDownLists")

    But that brings up a new error message, "Runtime error 9: Subscript out of range", regarding the following line of your code:

    .List = ws.ListObjects("tbl" & cboTypeMAIN).DataBodyRange.Value

    Would you happen to know what this is about?
    (If these are simple issues, I do apologize for being so clueless!)

    Thanks again -
    Hilary

  4. #4
    Valued Forum Contributor
    Join Date
    06-02-2012
    Location
    London, England
    MS-Off Ver
    365
    Posts
    397

    Re: Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    Is there definitely a table with the name of the item selected in the first ComboBox? And how many items are in the list of that table?

    Beth.

  5. #5
    Valued Forum Contributor
    Join Date
    06-02-2012
    Location
    London, England
    MS-Off Ver
    365
    Posts
    397

    Re: Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    I suspect at least one of your tables only has a single value and that's throwing up the error as you cannot add a single value to a ComboBox using .List

    However, as I mentioned earlier there are several ways of doing this, and here's another. This time looping through all the values in the tables and adding them one by one.
    Bit slower, but nothing you'll notice unless your tables have thousands of values.

    Replace the code you added earlier with this:
    Please Login or Register  to view this content.
    Beth.

  6. #6
    Registered User
    Join Date
    11-22-2019
    Location
    Victoria, Canada
    MS-Off Ver
    Office 365
    Posts
    6

    Re: Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    Hi Beth,

    I greatly appreciate your willingness to help here.

    Thank you for the second set of code. I did paste it in to replace the previous. It still brings up "Runtime Error 9", pointing to this line:
    Please Login or Register  to view this content.
    Just to clarify...Ever since the Dim ws as Worksheet line was put in, the userform does open correctly. And that "Runtime Error 9" only pops up when the "Type" field selection is actually made.

    You had asked about the lists:
    - there are definitely tables for each of the options in the "Type" list, with correctly named ranges that match that list
    - the "Type" list contains 15 items, including a blank at the beginning
    - the "Subtype" lists contain anywhere from 0 (actually 2 blank) to 14 rows
    - Runtime Error 9 comes up on every possible "Type" option, regardless of whether or not the associated named range has a blank in it

    Does that help narrow it down at all?

    Thank you -
    Hilary
    Last edited by AliGW; 12-21-2019 at 03:11 AM.

  7. #7
    Valued Forum Contributor
    Join Date
    06-02-2012
    Location
    London, England
    MS-Off Ver
    365
    Posts
    397

    Re: Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    Any chance we could see a desensitized version of the real file?

    Far easier to diagnose the issue if we can see it in context.

    Beth.

  8. #8
    Registered User
    Join Date
    11-22-2019
    Location
    Victoria, Canada
    MS-Off Ver
    Office 365
    Posts
    6

    Re: Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    Hi Beth,

    Here is all the VBA code from the UserForm module for the MAIN table. Could this help? (...rather than me posting the actual workbook...)

    This userform is still a work in progress on multiple fronts, and unfortunately you will be able to see clearly what a beginner I am with coding...
    It is more than likely that there is something I've put in there which is causing conflict with the code you have supplied...

    If this is too convoluted to look at, please let me know. I have already taken up much of your time...
    Thank you -
    Hilary
    Attached Files Attached Files

  9. #9
    Valued Forum Contributor
    Join Date
    06-02-2012
    Location
    London, England
    MS-Off Ver
    365
    Posts
    397

    Re: Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    We were all beginners at some point... Having looked at your code it's well written and doesn't look like beginner work, what it unfortunately doesn't tell me is what's causing the error.
    Would be much easier to find that with the workbook.

    Beth.

  10. #10
    Registered User
    Join Date
    11-22-2019
    Location
    Victoria, Canada
    MS-Off Ver
    Office 365
    Posts
    6

    Re: Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    Okay, that certainly does make sense. I'll need to take a bit of time to sort that out so I can post a clean version.

    In the meantime, in case it is relevant, the tables which are sitting on the "DropDownLists" sheet have actually been loaded in from another workbook via a query. I'm not sure if that would have an effect on this situation...

    For now, thanks very much for all of your assistance so far...

    Hilary

  11. #11
    Valued Forum Contributor
    Join Date
    06-02-2012
    Location
    London, England
    MS-Off Ver
    365
    Posts
    397

    Re: Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    No problem. Happy to help.

    I doubt the lists being imported via a query will be the issue, but cannot say for sure without seeing the file I'm afraid...

    Beth.

  12. #12
    Registered User
    Join Date
    11-22-2019
    Location
    Victoria, Canada
    MS-Off Ver
    Office 365
    Posts
    6

    Re: Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    Hi Beth,

    It’s working!

    With a little time to go over everything again, I discovered that I had in fact made the exact error that you had originally suspected.

    When you first mentioned it, I double-checked and compared all the list options with the table names and named ranges. But I managed to confuse the table/range names, so I had the table names matching the list options, rather than the named ranges matching them. Once I switched those out, it all worked beautifully.

    I just can’t thank you enough for your help. Both sets of code that you posted work perfectly. Thank you for seeing this through with me. This project can now move forward again!

    Sincerely,
    Hilary

  13. #13
    Valued Forum Contributor
    Join Date
    06-02-2012
    Location
    London, England
    MS-Off Ver
    365
    Posts
    397

    Re: Userform with Cascading Combo Boxes Based on Dynamic Named Ranges...

    Very glad I was able to help, Hilary.

    Good luck with the rest of the build and you know where we are if you get stuck.

    Beth.

+ 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. Dynamic Ranges for Combo Boxes
    By BONCH in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-19-2017, 07:16 PM
  2. [SOLVED] Draw line from named ranges based on userform combo box
    By Purgatorium in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-01-2016, 08:04 PM
  3. Replies: 3
    Last Post: 07-28-2014, 03:16 PM
  4. Replies: 5
    Last Post: 07-28-2014, 11:53 AM
  5. Cascading combo box and dynamic ranges
    By penfold1992 in forum Excel General
    Replies: 2
    Last Post: 08-24-2013, 06:33 AM
  6. [SOLVED] Adding dynamic named ranges to combo boxes in userform
    By Spritz in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-04-2013, 09:45 AM
  7. [SOLVED] Cascading Comboboxes With Multiple Dynamic Named Ranges?
    By spamad in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-20-2013, 10:52 AM

Tags for this Thread

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