+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    06-21-2009
    Location
    Rotterdam, The Netherlands
    MS-Off Ver
    Excel 2003, 2007
    Posts
    26

    Question searchmacro doen't work when initiated from listbox

    Hi there,

    I've been working on a database for a Dutch nursing home, but I'm struggling to get it to work. The file provides for a userform that enables users to search for residents and retrieve their appartmentnumber and the adress of their legal representative. This userform consists of three parts; in the first, the user can type the full or partial name of the resident of interest in a textbox; in the second phase, all matching registrations are presented in a listbox; and in the third phase, a macro searches for the name selected in the listbox and retrieves the corresponding appartmentnumber and adress. The first two phases of the userform work fine, but in the third phase, the macro fails to find any matches even though it is practically the same as the macro used in phase 1. I'd greatly appreciate it if anyone would take a look at the attached file and help me out here. Thanks in advance.
    Attached Files Attached Files
    Last edited by WCJanssen; 07-05-2009 at 04:28 AM.

  2. #2
    Registered User
    Join Date
    03-31-2004
    Location
    Toronto, Canada
    MS-Off Ver
    2003/2007
    Posts
    36

    Re: searchmacro doen't work when initiated from listbox

    Hey WCJanssen

    the search doesn't works because of the way you fill the list box with the code below
    Code:
    Private Function CvtRowValues(rowNumber As Long) As String
        Dim column As Integer
        CvtRowValues = ""
        For column = 1 To 1
            CvtRowValues = CvtRowValues & " " & Cells(rowNumber, column).Value
        Next
    End Function
    You are adding a space " " before the value so when you search using the selected item it's looking for value a space in front and so the search fails.

    You do not need For Next Loop in this case. Simply put

    Code:
    Private Function CvtRowValues(rowNumber As Long) As String
        Dim column As Integer
        CvtRowValues = ""
        CvtRowValues =  Cells(rowNumber, column).Value
    End Function
    You probably do not even need another private function. you can use your additem line as

    Code:
    ListBoxSpec.AddItem Cells(vFound.Row, 1).Value
    Another thing I noticed is that you do not need any of your Do While Loops, unless you posted a simplified version of your spreadsheet.

    Now for whatever reason if you want leave the space you can use LTrim(ListBoxSpec.Value) function under your Find statement for it to work.

    Hope this helps.

  3. #3
    Registered User
    Join Date
    06-21-2009
    Location
    Rotterdam, The Netherlands
    MS-Off Ver
    Excel 2003, 2007
    Posts
    26

    Thumbs up Re: searchmacro doen't work when initiated from listbox

    thanks a lot! I've replaced the function for the line you suggested and it works perfect.

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