+ Reply to Thread
Results 1 to 20 of 20

Populating Combo Box2 based on a selection made from Combo Box1

  1. #1
    Forum Contributor
    Join Date
    05-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007
    Posts
    144

    Populating Combo Box2 based on a selection made from Combo Box1

    Dear all

    Please, I need a VBA code that will populate Combo Box2 based on a selection made from Combo Box1.

    I have attached a sample file.

    My Combo Boxes 1 and 2, respectively, are not on a userform. They are just on a Worksheet.

    Looking at the file attached, The Combo Boxes are on Sheet1 and the Product Listings are on a different sheet named ‘Products’ (with the main Product Categories in column G and the Categpru/Description of the products in columns A and B).

    I wish to use the setup eventually for the purposes of a quick look-up of prices of products.

    I am aware that Data Validation would work OK. However, I wish to use Combo Boxes so I’ll be able to format/manipulate the lists in each combo box, hence I need a VBA code that will drive the combo boxes.

    As I searched through forums, I came across a vba code written by royUK for a purpose that is slightly identical to mine (although used on a userform). Here is the link:

    http://www.ozgrid.com/forum/showthre...t=55961&page=2

    And here is the code:
    Please Login or Register  to view this content.
    I use Excel 2007. I tried to adapt the code for my purpose. Unfortunately, I can’t get it to work for me. I should be very grateful for help with a vba code that will link the two combo boxes.

    Alternatively, if royUK’s code above could be tweaked to drive my two combo boxes on sheet 1, that will be great and I would also be grateful.

    Thanks.

    Newqueen
    Attached Files Attached Files

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi newqueen

    Create a Named Range called "Category" for Column A of "Products"'
    Please Login or Register  to view this content.
    Place this Code in Sheet1 Module...the Code is RoyUK Code adapted
    Please Login or Register  to view this content.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Forum Contributor
    Join Date
    05-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007
    Posts
    144

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi John,

    Thanks very much for your prompt and kind response.

    I followed your instructions/steps above, but unfortunately, I could not get it to work.

    I created the Named Range in column A of 'Products' sheet and assigned the offset formula as you instructed. I also copied and pasted the code into the Sheet1 Module. But nothing shows up in any of the combo boxes. I don't know what I am doing wrong.

    Is it possible you could test it all for me, please, using a copy of the sample file that I uploaded. If you get it to work, I'd be happy to use your working copy uploaded back.

    Thanks very much for your help.

    Newqueen

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi newqueen

    Attached is the sample File with the Code embedded.
    Attached Files Attached Files

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi newqueen

    Ha!!! I just realized why this is not working for you...I forgot to tell you the MOST important part!!!

    In the Developer Tab, Click on Design Mode...Right Click on Combobox1...Click on Properties...go down to ListFillRange...type in Main...see Image attached...Exit Design Mode.

    Sorry about that.
    Attached Images Attached Images

  6. #6
    Forum Contributor
    Join Date
    05-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007
    Posts
    144

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi John,

    That was brilliant! Thank you so very much. You are very kind and helpful. I am truly grateful.

    Newqueen

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi newqueen

    You're welcome...glad I could help. Thanks for the Rep.

  8. #8
    Forum Contributor
    Join Date
    05-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007
    Posts
    144

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi John,

    Re my subsequent request, I should be grateful if you would, please, help me with the steps I should take to successfuly place a vlookup formula in Sheet1 Cell K5 so as to be able to pick up a price from the 'Products' sheet, column D, as earlier mentioned to you in my PM.

    Thank you for your continued support.

    Newqueen

  9. #9
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi Clare

    Please note the Code in General Module 1. This line of Code can be in any General Module but it must be at the very top of that Module
    Please Login or Register  to view this content.
    Please note the changes in this Code
    Please Login or Register  to view this content.
    And the addition of this Code
    Please Login or Register  to view this content.
    Let me know of issues.
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    05-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007
    Posts
    144

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi John,

    Your VBA solution was spot on! Thanks heaps.

    Just a few questions, please: Do you think that a vba code would be better than a vlookup formula for that purpose?

    Also, if in future, I add more items to the 'Products' sheet both in the main 'Categories' list i.e. column G, and sort the list, and also add entries to columns A to D, and also sort (based on column A), will the sorting of those lists affect the outcome in Sheet1 in any way?

    Grateful for you kind clarification, please.

    newqueen
    Last edited by newqueen; 09-03-2014 at 11:13 PM.

  11. #11
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi Clare

    I have no opinion regarding this
    Do you think that a vba code would be better than a vlookup formula for that purpose
    Regarding this...all the Named ranges are Dynamic so this should not be an issue
    I add more items to the 'Products' sheet both in the main 'Categories' list i.e. column G, and sort the list, and also add entries to columns A to D
    Sorting should also not be an issue...try adding items...and sorting...let me know if you have a problem.

  12. #12
    Forum Contributor
    Join Date
    05-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007
    Posts
    144

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Thanks John for your kind response. Not that using a vba code in place of vlookup matters that much. I only wanted to seek opinion on which option might be trouble-free, easier to work with and, therefore better.

    Meanwhile, I'll try the sorting. I am not looking forward to any issues with that.

    Thank you for everything.

    Newqueen

  13. #13
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi Clare

    I'd suggest you pose this question in the Formulas Forum...you'll get a much better educated response from the Formula Gurus.

  14. #14
    Forum Contributor
    Join Date
    05-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007
    Posts
    144

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi John,

    Pleeeaaasee, just one final look at this for me. Sorry for bothering you.

    For reasons, I am still looking at the possibility of using a VLOOKUP formula, sometime, for the look-up cell. In consideration of that, I have used your first solution and tried to apply a Vlookup formula for the price output cell (F5) - (See attached), but I keep getting an error message. Please kindly have a look at it fpr me.

    I certainly am VERY pleased with the vba code version for the price lookup. I want to be sure that I'm able to use a vlookup for it if and when the need arises. I also want to learn from you regarding this.

    I am aware that in order to use a vlookup for an ActiveX control combobox, there has to be a linked cell. I have, for this purpose, used Cell I2 as the linked cell. But when I click to choose from Combobox 2, I get a debug message and the 'me.combobox2.clear' line of the vba code gets highlighted yellow - see attached file.

    Thanks for everything.

    Newqueen
    Attached Files Attached Files
    Last edited by newqueen; 09-04-2014 at 06:44 PM.

  15. #15
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi Clare.

    Damn you pose difficult problems.

    I am useless with sheet based Comboboxes

    So I have spent three days [ Three Days ] trying to get my head around this.

    There are three types of sheet based comboboxs [ I didn't know that ]. All with different pros and cons.

    So I created a couple of macros to help you.

    PS I didn't like your layout.

    Ok my first routine runs when you activate sheet one. It finds how many rows are used in column A.
    And uses that number to populate the categories combobox.
    This means that you can add categories by simply typing in column A.

    I didn't write it but you could use a sheet change macro to create the table for any new category.

    The second macro runs when you change the categories combobox
    It counts the number of products in the selected category and populates the second list box accordingly.
    This means that you can add categories by simply typing in the data column.


    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by mehmetcik; 09-06-2014 at 06:23 AM. Reason: Macro added to sheet "Products"

  16. #16
    Forum Contributor
    Join Date
    05-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007
    Posts
    144

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi John,

    Thanks very much. I am truly sorry that my request (my Post #14) has caused you difficulties. It wasn't intended. I am appreciative of all the time and effort that you have given to this.

    I have looked at the new approach you adopted (re your post #15 above). While it is good, I believe that it would be too complicated for me. My knowledge of vba is very much at the baby stage or, at best, non-existent.

    Infact, your solution (re your Post #9 above) is very neat and, in my view, simple enough for me to work with. Moreso, if there is the need for new categories and their related items, they can be added easily without going through a macro.

    Everything about your solution in your post #9 above is very good. My only additional desire was to explore how, if it is possible, I can use a VLOOKUP formula (instead of a vba code) for Cell K5 ONLY so as to output the product price into that cell. If that was possible, that is the only change that I was requesting for.

    It would be great if I can get and use a workable VLOOKUP formula for Cell K5. I would still like to explore that option. If I can get help with that, that will be fine. The fact that you used a vba code for that purpose does not, in any way, diminish the brilliant work you did. And I remain very grateful to you .

    Kind Regards.

    Newqueen

  17. #17
    Forum Contributor
    Join Date
    05-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007
    Posts
    144

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Could someone please help with the VLOOKUP part of this (See post #14 above).

    Thanks.

  18. #18
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Hi Clare

    Please note, Post #15 is NOT my input

    Attached are two samples of how you may accomplish the VLOOKUP.

    In BOTH Sample Files this Dynamic Named Range myTable has been added
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    In SAMPLE File:
    • Combobox2, Properties, Linked Cell has been set to L5
    • Cell K5 has this Formula
      Formula: copy to clipboard
      Please Login or Register  to view this content.
    • The Sheet Code has been modified

    In SAMPLE v1 File only the Sheet Code has been modified.

    Pick your poison.
    Attached Files Attached Files

  19. #19
    Forum Contributor
    Join Date
    05-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007
    Posts
    144

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    Dear John,

    I'm so very sorry for the mix-up! Pardon me, please.

    Thanks too for coming back to me with the two additional options. After trying the two new options ('poisons' according to you), and noting the problems with each of them, I think I might just stick with your all-vba and flawless solution (re your post #9).

    In my view, it appears that the VLookup formula cannot be successfully used with two dependent ActiveX Control Comboboxes.

    Just for information - The issues that I found with the two options are as follows:

    Option 1: This option incorporates the Vlookup formula that I desired. However, when you choose a category from combobox1, the category name appears on the box. That's fine. But when you choose its related item from combobox 2, NOTHING appears on combobox2. Rather the item appears only in the linked cell (which is the source of the vlookup). Given that the linked cell would normally be hidden, it would have been nice if the item chosen from combobox2 would appear both in combobox2 and in the linked cell.

    With combobox2 remaining blank when an item is chosen from it doesn't look nice. With this option, the ideal situation would be that both combobox2 and the linked cell (L5) should only go blank when a new category is chosen from combobox1.

    Option 2: This option is all vba as in #9. When a new category choice is made from combobox1, the contents of combobox2 are cleared (which is good)! But K5 is not cleared until a choice is made from combobox2. This was very well taken care of in your post #9 where combobox2 and K5 automatically become blank upon choosing a new category from combobox1.

    Therefore, in the absence of any method of nicely linking a Vlookup formula to two such comboboxes, I'll stay with your original all-vba option (#9) which I found very good, and it did the job neatly.

    Again, thanks very much for all your help. You've been very wonderful.

    I also wish to thank my good friend Mehmetcik for his input. Grateful.

    newqueen
    Last edited by newqueen; 09-06-2014 at 05:50 PM.

  20. #20
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Populating Combo Box2 based on a selection made from Combo Box1

    You don't need a vlookup.

    This is all you need:

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by mehmetcik; 09-06-2014 at 06:53 PM.

+ 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. [SOLVED] Combo Box2 selections available based on Combo box 1 selection
    By wishn4fishn in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-28-2013, 08:13 PM
  2. combo box input range based on the other combo box selection
    By hedayet in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-08-2013, 02:27 AM
  3. Excel - populating data (checkboxes) based on combo box selection
    By gkang116 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-28-2011, 06:50 PM
  4. Populate a Combo Box based on the selection of another Combo Box
    By thunder279 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-18-2011, 03:40 PM
  5. Replies: 2
    Last Post: 03-20-2006, 09:10 PM

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