+ Reply to Thread
Results 1 to 18 of 18

Userform: Unique Values to ComboBox

  1. #1
    Registered User
    Join Date
    09-21-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    41

    Userform: Unique Values to ComboBox

    For instance say I have a worksheet called "Pets"

    This worksheet contains the following pet names from ranges A1 : A10

    Dog
    Cat
    Mouse
    Lizzard
    Dog
    Cat
    Mouse
    Dinosaur
    Lion
    Tiger

    Notice how there are repeating values. How can I copy the UNIQUE values from this worksheet and copy it into a comboBox using VBA codes (preferabbly using Add Items).

    I know that to begin with, we will have to declare an object variable to define the range and the worksheet variable.
    After designing the comboBox Userform, How to we add in values, specifically UNIQUE values

    Thank you in advance!

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Userform: Unique Values to ComboBox

    Hi, dinosaur1993,

    maybe like this:
    Please Login or Register  to view this content.
    Please alter the names of the sheet as well as the name of the combobox to suit.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    09-21-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Userform: Unique Values to ComboBox

    Thank You Holger! But lets say what if the user constantly comes back to the spreadsheet and adds on to the lists from time to time?

    Would this work?

    Please Login or Register  to view this content.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Userform: Unique Values to ComboBox

    Hi, dinosaur1993,

    basicly your code is correct. I think you would move the code for setting the range into the With-Statement to have it work for Sheets(1) as well. And Rng is an element on itīs own, if you put it in like you did VBA doesnīt takre it as the range but as a variable:
    Please Login or Register  to view this content.
    Ciao,
    Holger

  5. #5
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Userform: Unique Values to ComboBox

    Hi, dinosaur1993,
    as an option
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    09-21-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Userform: Unique Values to ComboBox

    What is the purpose for using the CountIf function?


    Quote Originally Posted by HaHoBe View Post
    Hi, dinosaur1993,

    basicly your code is correct. I think you would move the code for setting the range into the With-Statement to have it work for Sheets(1) as well. And Rng is an element on itīs own, if you put it in like you did VBA doesnīt takre it as the range but as a variable:
    Please Login or Register  to view this content.
    Ciao,
    Holger

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Userform: Unique Values to ComboBox

    Hi, dinosaur1993,

    count the number of occurrances of one item in a given range - itīs the function you may use as a formula Countif in your excel sheet as well.

    Ciao,
    Holger

  8. #8
    Registered User
    Join Date
    09-21-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Userform: Unique Values to ComboBox

    Oh, ok...

    Why is it that for some reason, I am still getting a lists of whatever is in A1: A10?
    Also, say for instance, I also want to put them in alphabetical order within the comboBox, is that possible?

    Thanks In Advance

  9. #9
    Registered User
    Join Date
    09-21-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Userform: Unique Values to ComboBox

    Thank you Nilem! However, I dont really get what these line of code is doing?

    Quote Originally Posted by nilem View Post
    Hi, dinosaur1993,
    as an option
    Please Login or Register  to view this content.

  10. #10
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Userform: Unique Values to ComboBox

    Hi, dinosaur1993,

    regarding the list not covering only uniques: check the entries for leading and/or trailing sblanks.

    About sorting: either wirte to a new workbook, sort there, read from there or use an array and sort that. Thirs option could be using a Dictionary or Collection.

    Regarding nilemīs code: checking each cell value againts the string which was created and only add teh cell value if itīs not present in the string. InStr will retrun the position of the character of the forund item in the string, if 0 no match was found so add.

    Ciao,
    Holger

  11. #11
    Registered User
    Join Date
    09-21-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Userform: Unique Values to ComboBox

    Pets.xlsm

    Ok, So this is what I have been trying to work on, but for some reason, the code doesn't really work right. Thanks

  12. #12
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Userform: Unique Values to ComboBox

    Hi, dinosaur1993,

    the code doesn't really work right


    You set the range starting at D5 but start the loop at row 1 - when altered that I get the result I would expect from it:
    Please Login or Register  to view this content.
    Ciao,
    Holger

  13. #13
    Registered User
    Join Date
    09-21-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Userform: Unique Values to ComboBox

    Hey Holger,

    I have tried that initially, but I still end up with the same thing. Sorry for being annoying

    Kind Regards,
    Dinosaur1993

  14. #14
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Userform: Unique Values to ComboBox

    Hi, dinosaur1993,

    the original solutions from both nilem and me make use of the UserForm_Initalize-event which is only triggered once: right at the point where the UserForm is created for use in the worksheet environment. You changed the code to an event which may be triggered multiple times and wonder why the code adds the items which is whatīs written in the code. So we better clear the contents before reading and adding again.

    You should alter your procedure to read:
    Please Login or Register  to view this content.
    Ciao,
    Holger

  15. #15
    Registered User
    Join Date
    09-21-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Userform: Unique Values to ComboBox

    oh btw, when I said unique values, I want it to include each of the repeating values as well..

    Thanks!

  16. #16
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Userform: Unique Values to ComboBox

    Hi, dinosaur1993,

    if that requirement should be satisfied you may not compare the item to the range but only to the range from the start to the present row:
    Please Login or Register  to view this content.
    Ciao,
    Holger

  17. #17
    Registered User
    Join Date
    09-21-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Userform: Unique Values to ComboBox

    Thank you very much Holger! You have been great help!

    What If rather than Pet Names, I were to use Pet codes instead, the code has to change yeah? Instead of using .Value, what do we have to use, or is it just the same thing?

    Cheers!
    Dinosaur1993

  18. #18
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Userform: Unique Values to ComboBox

    Hi, dinosaur1993,

    just change the reference form Column D to Column C, and leave the rest alone (maybe alter the label caption as well).

    Please Login or Register  to view this content.
    Ciao,
    Holger

+ 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] Changed userform combobox to listbox, unable to get userform to retrieve datasheet values
    By dragonabsurdum in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-29-2013, 01:38 PM
  2. populate a combobox based on unique values in another workbook
    By roninn75 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-03-2013, 03:36 AM
  3. [SOLVED] ComboBox To Reflect Unique Values
    By Doctor_H in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2013, 03:36 PM
  4. fill combobox with unique values.
    By ali84pk in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-01-2011, 06:23 AM
  5. how to populate a combobox with a list of unique values?
    By RIOSGER in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-09-2005, 12:05 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