+ Reply to Thread
Results 1 to 10 of 10

displaying text box search results in a listbox

Hybrid View

Nitefox displaying text box search... 06-23-2014, 08:18 PM
6StringJazzer Re: displaying text box... 06-23-2014, 08:36 PM
Nitefox Re: displaying text box... 06-23-2014, 10:16 PM
Nitefox Re: displaying text box... 06-26-2014, 07:19 PM
6StringJazzer Re: displaying text box... 06-26-2014, 09:08 PM
Nitefox Re: displaying text box... 06-27-2014, 01:08 AM
humdingaling Re: displaying text box... 06-27-2014, 01:51 AM
6StringJazzer Re: displaying text box... 06-27-2014, 07:26 AM
Nitefox Re: displaying text box... 06-28-2014, 03:14 AM
humdingaling Re: displaying text box... 06-29-2014, 08:38 PM
  1. #1
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    displaying text box search results in a listbox

    I am trying to create a search where the user types into the text box 'ItemDescription' then hits the 'ItemDescSearch' button (see below code) to pollute the list box 'lbSamDesc' with any partial matches from the specified range. Currently when I click on the button it takes about 8 seconds then no results are displayed in the List Box. What am I doing wrong?

    Private Sub ItmDescSearch_Click()
    
        Dim c As Range, f As Range
        lbSamDesc.Clear
        For Each c In Worksheets("EquipmentData").Range("F6", Worksheets("EquipmentData").Range("F" & Rows.Count).End(xlUp))
        
            Set f = c.Find(ItemDescription.Value, lookat:=xlPart)
            If Not f Is Nothing Then
                lbSamDesc.AddItem c.Value
            End If
        Next c
        
    End Sub

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,762

    Re: displaying text box search results in a listbox

    Impossible to know without seeing your data.

    Although your code may work (I don't see anything obviously wrong with it), you are combining two methods that are not optimal when used like this. Find is very powerful for finding matches in an entire range of cells, but you are using Find one cell at a time. The following modification should speed things up but I can't tell why your code doesn't work without seeing your data, and I can't do a decent test without your file. I suggest you attach it.

    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("F6", Worksheets("EquipmentData").Range("F" & Rows.Count).End(xlUp))
        
        Set f = RangeToSearch.Find(ItemDescription.Value, lookat:=xlPart)
        Set fFirst = f
        
        Do Until f Is Nothing
        
            If f.Address = fFirst.Address Then Exit Do
            
            lbSamDesc.AddItem c.Value
            f = RangeToSearch.FindNext(f)
            
        Loop
        
    End Sub
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: displaying text box search results in a listbox

    Thank your for the response.
    I tried that code but it still doesn't seem to do anything for me. However I did notice that whatever it is doing it only took a second (verses the last code taking several).
    I have attached a sample workbook with the data and userform.

    Thanks,
    James
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: displaying text box search results in a listbox

    Any thoughts Jeff?

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,762

    Re: displaying text box search results in a listbox

    Couple of bugs in the code, my fault. Providing your file made it possible to diagnose and resolve.

    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
        
    End Sub

  6. #6
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: displaying text box search results in a listbox

    Thanks Jeff that worked great (sorry I should have provided a file from the beginning).
    Is it possible to have it remove the duplicates from the results?

    Cheers,
    james

  7. #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.

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,762

    Re: displaying text box search results in a listbox

    Nice code humdingaling--but Nitefox, wouldn't it be easier to just filter out duplicates from the original data?

  9. #9
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: displaying text box search results in a listbox

    Thanks humdingaling that did it.

  10. #10
    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

    Thanks both

    agree with 6string
    would be much easier to just clean original data set of duplicates

    especially if you are going to branch out off this for other parts of potential project

+ 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] 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