Consulting

Results 1 to 19 of 19

Thread: Fill a list box with unique items

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Fill a list box with unique items

    Re this item http://www.vbaexpress.com/forum/showthread.php?t=4107 I'm looking for a way to fill the listbox with unique items from Column C plus another item "All"
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mdmackillop
    Re this item http://www.vbaexpress.com/forum/showthread.php?t=4107 I'm looking for a way to fill the listbox with unique items from Column C plus another item "All"
    Malcolm,

    I would use a dictionary object. Load that with an All item to start, then loop through C loading all items. Duplicates get ignored.

    An alternative is to advance filter column C with Unique records and to a new place, just under an item of All.

    I won't bother with the code, I am sure you can handle whichever, if any, you choose
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Bob,
    Never actually used the dictionary object, but it looks simple enough. Only problem is
    with
    Dim a, d 'Create a variable
    Set d = CreateObject(Scripting.Dictionary)
    For Each Cell In Range("C2:C" & [c65536].End(xlUp))
    i = i + 1
    d.Add i, Cell.Text
    Next
    I'm getting "Method or Data Member not found" with Dictionary highlighted.
    I've added Scripting Runtime, and Script Control as references. Am I missing something?
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location
    What is a dictonary object.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Do you know, I knew I had your name wrong, but I had that in my mind and went ahead with it. Should have known you had a good Scots name. Apologies for that.

    Anyway, the error. It needs to be enclosed in quotes.


    Set d = CreateObject("Scripting.Dictionary")

    And preced it with On Error Resum Next in the real version, to ignore the duplicates.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by gsouza
    What is a dictonary object.
    http://msdn.microsoft.com/library/de...filesystem.asp
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why not use a Collection?

  8. #8
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by Norie
    Why not use a Collection?
    I believe the Dictionary Object is faster.

    Another way (understandably debatable) is to use Advanced Filter, possibly coupled with an array.

  9. #9
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    I like to use the dictonary object because it has a .Exist that returns a boolean, then based on the return, you do what you gotta do, saving the error checking for my other brain cramps . IMHO The collections are actually slower.

  10. #10
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by gsouza
    What is a dictonary object.
    http://msdn.microsoft.com/library/de...dictionary.asp
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Tommy
    I like to use the dictonary object because it has a .Exist that returns a boolean, then based on the return, you do what you gotta do, saving the error checking for my other brain cramps .

    Sub TestCollection()
    Dim col As Collection
        Set col = New Collection
        col.Add "Me", "xld"
        MsgBox IfExists(col, "xld")
    End Sub
    
    Public Function IfExists(col As Collection, ByVal sKey As String)
    On Error GoTo NoSuchKey
    If VarType(col.Item(sKey)) = vbObject Then
             ' force an error condition if key does not exist
         End If
    IfExists = True
    Exit Function
    NoSuchKey:
         IfExists = False
    End Function

    Great thing about collections are that you have have a collection class for our custom objects.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by xld
    Anyway, the error. It needs to be enclosed in quotes.
    Thanks Bob,
    I foolishly copied the code from Excel Help, thinking that it would be correct!
    Works well.
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    Knowledge Base Approver VBAX Expert brettdj's Avatar
    Joined
    May 2004
    Location
    Melbourne
    Posts
    649
    Location
    Quote Originally Posted by Tommy
    I like to use the dictonary object because it has a .Exist that returns a boolean, then based on the return, you do what you gotta do, saving the error checking for my other brain cramps . IMHO The collections are actually slower.
    Bingo

    Malcolm, I use both Dictionary and Collection examples in http://www.vbaexpress.com/kb/getarticle.php?kb_id=67

    Cheers

    Dave

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Dave,
    Your entry on the KB list is as below, so unfortunately looking at this list would not get me to your entry. Are we needing to set Keywords into the description to make the KB more searchable.
    ExcelSimulate a Poker GameThis code inserts a new worksheet and deals a 5-Card Poker hand to 10 players. The code requires Excel 2000 or higher.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  15. #15
    Knowledge Base Approver VBAX Expert brettdj's Avatar
    Joined
    May 2004
    Location
    Melbourne
    Posts
    649
    Location
    hmm - don't keywords in the KB Description get used for searching?

  16. #16
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Quote Originally Posted by brettdj
    Bingo

    Malcolm, I use both Dictionary and Collection examples in http://www.vbaexpress.com/kb/getarticle.php?kb_id=67

    Cheers

    Dave
    That is a wonderful tips.
    Thx.

  17. #17
    VBAX Mentor XL-Dennis's Avatar
    Joined
    May 2004
    Location
    ?stersund, Sweden
    Posts
    499
    Location
    The only objection I have with the Scripting approach is that it may cause unnessecary overhead compared with the built-in Collection. For small solution this is not an issue while for large application it may be.

    In my experience many large worldwide clients do not accept the use of this library due to security. Within some companies I've been working with the library is not available at all.

    Looping through a range is slower then reading the range values into a variant variable and then populate the choosen collection.

    Kind regards,
    Dennis
    Kind regards,
    Dennis

    ExcelKB | .NET & Excel | 2nd edition PED


  18. #18
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks all!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  19. #19
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    The site search engine and the KB search engine are completely different, and at the moment do not cross-reference each other. The KB Search engine is custom scripted by smozgur.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •