Results 1 to 10 of 10

displaying text box search results in a listbox

Threaded View

  1. #7
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: displaying text box search results in a listbox

    adapted from
    http://j-walk.com/ss/excel/tips/tip47.htm


    Private Sub ItmDescSearch_Click()
    
        Dim c As Range, f As Range
        lbSamDesc.Clear
        Dim RangeToSearch As Range
        Dim fFirst As Range
        
        Set RangeToSearch = Worksheets("EquipmentData").Range("C3", Worksheets("EquipmentData").Range("C" & Rows.Count).End(xlUp))
        
        Set f = RangeToSearch.Find(ItemDescription.Value, lookat:=xlPart)
        Set fFirst = f
        
        Do Until f Is Nothing
                
            lbSamDesc.AddItem f.Value
            Set f = RangeToSearch.FindNext(f)
            
            If f.Address = fFirst.Address Then Exit Do
            
        Loop
    
        Dim i As Long, j As Long
        Dim nodupes As New Collection
        Dim Swap1, Swap2, Item
        
        With lbSamDesc
        
            For i = 0 To .ListCount - 1
                ' The next statement ignores the error caused
                ' by attempting to add a duplicate key to the collection.
                ' The duplicate is not added - which is just what we want!
                On Error Resume Next
                nodupes.Add .List(i), CStr(.List(i))
            Next i
       
        '   Resume normal error handling
            On Error GoTo 0
        
            'Clear the listbox
            .Clear
            
            'Sort the collection (optional)
            For i = 1 To nodupes.Count - 1
                For j = i + 1 To nodupes.Count
                    If nodupes(i) > nodupes(j) Then
                        Swap1 = nodupes(i)
                        Swap2 = nodupes(j)
                        nodupes.Add Swap1, before:=j
                        nodupes.Add Swap2, before:=i
                        nodupes.Remove i + 1
                        nodupes.Remove j + 1
                    End If
                Next j
            Next i
        
        '   Add the sorted and non-duplicated items to the ListBox
            For Each Item In nodupes
                .AddItem Item
            Next Item
            
        End With
    
    End Sub
    Attached Files Attached Files
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Search for Text and displaying the results
    By ymaster44 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2014, 12:20 PM
  2. [SOLVED] [Help] I need search results in listbox reversed
    By Compo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-11-2013, 07:22 AM
  3. Search Results displayed in a Listbox
    By Julesdude in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-01-2011, 08:49 AM
  4. displaying search results after searching 2 cbo's
    By ncaravela in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-31-2010, 08:19 AM
  5. Displaying Search results (filtering?)
    By Klok in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-04-2009, 06:45 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