+ Reply to Thread
Results 1 to 15 of 15

populate listbox with unique text

  1. #1
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

    populate listbox with unique text

    Hello all,

    My goal is to populate a listbox (listbox3) in a user form with unique text items that appear in Column A of my datasheet. The code below is able to do what I want, albeit very slowly (right now there are 20,000 rows of data I need to select unique items from). Are there any alternatives that might speed this up?

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: populate listbox with unique text

    Think you can attach a sample file?

  3. #3
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

    Re: populate listbox with unique text

    I'm getting an error with the file upload, but Column A looks like this:

    country
    US
    US
    US
    US
    US
    US
    US
    UK
    UK
    UK
    UK
    UK
    UK
    Italy
    Italy
    Italy
    Italy
    Italy
    Italy
    Italy
    Italy
    Italy
    Italy
    Italy
    US/CAN
    US/CAN
    US/CAN
    US/CAN
    US/CAN
    US/CAN
    US/CAN

  4. #4
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

    Re: populate listbox with unique text

    accidental double post...
    Last edited by mcdermott2; 04-02-2013 at 03:17 PM.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: populate listbox with unique text

    Try
    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: populate listbox with unique text

    Same thing as jindon's except I used collection

    Please Login or Register  to view this content.
    Attached Files Attached Files

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

    Re: populate listbox with unique text

    Hi

    Both the above examples are good.

    However there is a lot of reading and writing from excel to VBA which will slow the process down.

    I would read all the column into VBa as an array.

    Please Login or Register  to view this content.
    Transfer the unique items to a collection to ensure only unique items exist.

    I would then write the Collection back to excel.

    Please Login or Register  to view this content.
    As there is only one read and one write in the Macro. It should be really fast.

  8. #8
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

    Re: populate listbox with unique text

    Thanks! jindon- your code worked perfectly (I got an error because I am in idiot and had changed the tab name), but JieJenn, yours worked like a charm. I really appreciate both of your input!
    Last edited by mcdermott2; 04-02-2013 at 03:49 PM.

  9. #9
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: populate listbox with unique text

    You might want to develop this formula derived solution.
    It uses a mobile named range for the drop-downs in Column E based on the unique values generated in Column D.

    As it stands it isn't portable, but this can be addressed if you want to pursue this method.

    Column A must be grouped, but not nescessarily sorted.
    Attached Files Attached Files
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: populate listbox with unique text

    my code
    1)
    Please Login or Register  to view this content.
    is actually looping inside the array, not cells, so it should be very fast even 20000 cells.

    2)
    If you want the unique in the range. add one line
    Please Login or Register  to view this content.

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

    Re: populate listbox with unique text

    This Code should be fast. So Enjoy.


    Please Login or Register  to view this content.

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

    Re: populate listbox with unique text

    Thanks Jindon.

    I will try your code.

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: populate listbox with unique text

    Quote Originally Posted by mcdermott2 View Post
    Thanks! jindon- your code gave me a "Run-time error '9': Subscript out of range,
    Then you need to check the sheet name, I copied it from your original code.

  14. #14
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

    Re: populate listbox with unique text

    mehmetcik's code works perfectly if the "=" is dropped from
    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

    Re: populate listbox with unique text

    Quote Originally Posted by jindon View Post
    Then you need to check the sheet name, I copied it from your original code.
    Yes, my mistake. Thanks for the help!

+ 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