+ Reply to Thread
Results 1 to 16 of 16

Search all records in 3 columns via a userform and display the results in my listview

Hybrid View

  1. #1
    Registered User
    Join Date
    04-01-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Search all records in 3 columns via a userform and display the results in my listview

    I have been able to, with the kind help and assistance of various resources in this forum, managed to create a userform that will search selected columns in my workbook via a userform and display the results in a listview.

    I am trying to amend this so that I can search through all columns and display the results on my userform listview and not having to select a specific column.

    I attach my workbook to where I have managed to get upto so far and am asking whether anyone knows how I can amend the code so that the searcg works for all columns, not just the one that I select via my combobox.

    Please can anyone assist me with any help or guidance?

    Many thanks for taking the time to read my post, it is much appreciated.
    Attached Files Attached Files
    Last edited by pells; 02-07-2012 at 11:31 AM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi pells

    This has been tested ONLY perfunctorily. Let me know of issues.
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    04-01-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi jaslake,

    Many thanks for taking to time to have a look at this.

    Unfortunately, it doesnt seem to work as when I run the form and enter something into the Search Term, I am prompted to enter is a search category. What I was hoping to achieve was to enter in a search term and the results would appear without having to select a category.

    Hope this makes sense?

    Many thanks.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi pells
    Sorry, I misunderstood. I didn't clean the code up but something like this may work for you
    Private Sub CommandButton1_Click()
    'SEARCH
        Dim Cnt As Long
        Dim Col As Variant
        Dim FirstAddx As String
        Dim FoundMatch As Range
        Dim LastRow As Long
        Dim R As Long
        Dim StartRow As Long
        Dim Wks As Worksheet
        Dim rng As Range
        Dim C As Range
        Dim SearchRecords As Long
        Dim i As Long
        Dim colCnt As Long
    
        StartRow = 4
    
        If TextBox1.Text = "" Then
            MsgBox "Please enter a search term."
            TextBox1.SetFocus
            Exit Sub
        End If
    
        Set Wks = ActiveWorkbook.Sheets("Live Job List")
        colCnt = Wks.Cells(4, Columns.Count).End(xlToLeft).Column
        For i = 1 To colCnt
            Col = i
            LastRow = Wks.Cells(Rows.Count, Col).End(xlUp).Row
            LastRow = IIf(LastRow < StartRow, StartRow, LastRow)
    
            Set rng = Wks.Range(Wks.Cells(1, Col), Wks.Cells(LastRow, Col))
    
            Set FoundMatch = rng.Find(What:=TextBox1.Text, _
                    After:=rng.Cells(1, 1), _
                    LookAt:=xlPart, _
                    LookIn:=xlValues, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False)
    
            If Not FoundMatch Is Nothing Then
                FirstAddx = FoundMatch.Address
                ListView1.ListItems.Clear
    
                Do
                    Cnt = Cnt + 1
                    R = FoundMatch.Row
    
                    ListView1.ListItems.Add Index:=Cnt, Text:=Wks.Cells(R, 1)
                    For Col = 2 To 3
                        Set C = Wks.Cells(R, Col)
                        ListView1.ListItems(Cnt).ListSubItems.Add Index:=Col - 1, Text:=C.Text
    
                    Next Col
    
                    Set FoundMatch = rng.FindNext(FoundMatch)
                Loop While FoundMatch.Address <> FirstAddx And Not FoundMatch Is Nothing
                SearchRecords = Cnt
    
            Else
                '             ListView1.ListItems.Clear
                '             SearchRecords = 0
                '             MsgBox "No match found for " & TextBox1.Text
            End If
        Next i
    End Sub

  5. #5
    Registered User
    Join Date
    04-01-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi jaslake,

    Many thanks for this.

    I have tried the code, but unfortunately I get an error:

    Run-time error '35600': Index out of Bounds

    On the line: ListView1.ListItems.Add Index:=Cnt, Text:=Wks.Cells(R, 1)

    Any ideas?

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi pells
    change this line of code
    Set FoundMatch = rng.Find(What:=TextBox1.Text, _
                    After:=rng.Cells(1, 1), _
                    LookAt:=xlWhole, _
                    LookIn:=xlValues, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False)

  7. #7
    Registered User
    Join Date
    04-01-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi jaslake,

    Many thanks, there are no errors now, but you have to be specific in the search e.g. you have to put in Smith to return results for Smith. When the combo box was selected for PM and I entered in "SM", I would get all the PM's where SM is in their name.

    Is there a way of getting this to work so that it searched for anything I put into the search term. I thought by setting the LookAt:=xlPart does this but when I replaced this with xlWhole as you suggest, I get the error again.

    Is there a way of doing this without the error?

    Once again, many thanks for all your help!

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi pells
    If you comment out this line of code (actually I'd move it outside the For...Next loop)
    If Not FoundMatch Is Nothing Then
                FirstAddx = FoundMatch.Address
    '            ListView1.ListItems.Clear
    and change xlWhole back to xlPart you'll get the results you expect searching on "SM" BUT you'll get unwanted results when you search on "1"

  9. #9
    Registered User
    Join Date
    04-01-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Search all records in 3 columns via a userform and display the results in my list

    Many thanks jaslake - this does work as you detail, excellent, you have been a great help to me here! But how come I get unwanted results when, for example, you search on "1"?

    Once again, many thanks.

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi pells...perhaps I assumed too much. Searching on "1" also pulls in the "10"s but that may well be what you were looking for.

  11. #11
    Registered User
    Join Date
    04-01-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi jaslake - no i think you have assumed correctly.

    If i enter in "1", the results are duplicated x2 then a funny result as Job 2 is also appearing, when it shouldnt as there isnt a "1" anywhere in this entry......

    Any ideas, many thanks?

  12. #12
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi pells...here's an image of what I get searching on "1"
    screen shot.jpg

  13. #13
    Registered User
    Join Date
    04-01-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi jaslake - how strange, I get that too now!!!

    Do you know why there are duplicates for the same records? I would expect the results only to show Job 1 and Job 10, so the search find any "1" across 3 columns and only provides one result per record.

    Many thanks.

  14. #14
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi pells
    Your searching on xlPart so, the procedure finds the "1"s in column 1 and also finds the "1"s in column 2 (the digit 1 in job 1 and the digit 1 in job 10) xlPart...understand?

  15. #15
    Registered User
    Join Date
    04-01-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi jaslake - yes I undetrstand.

    Many thanks for everything you have been a great help!

    Pells.

  16. #16
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search all records in 3 columns via a userform and display the results in my list

    Hi pells

    You're welcome...glad I could 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