Results 1 to 7 of 7

Thread: [RESOLVED] search form in excel range only

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    Resolved [RESOLVED] search form in excel range only

    Hi guys and or gals,

    I'm trying to create a custom seach form for an excel workbook, I got a simple one to work, but i need to get the search within specific ranges, I tried getting the below code to work, but I'm bashing my head against the wall...

    Any help would be awesome!

    Basically the form has three values

    txtName
    txtChange
    Txtdate
    the form should be able to able to search one of the other, none are manditory. I've created the ranges as mentioned in the below code but it keeps falling over...

    Code:
    Private Sub Cmd_OK_Click()
    Dim MyText As String
    Dim MyMsg As String
    Dim MyType
    Dim MyTitle As String
    Dim rfoundcell As Range
    Dim lcount As Long
    
    
    MyText = txtname.Value
    MyText = txtchange.Value
    MyText = txtdate.Value
    MyMsg = "No matches were found."
    MyType = vbOKOnly + vbExclamation
    MyTitle = "No Matches"
    '
    Set rfoundcell = Range("Name")
    Set rfoundcell = Range("Change")
    Set rfoundcell = Range("Date")
    
    On Error GoTo ErrorHandler
    
    For lcount = 1 To WorksheetFunction.CountIf(Columns(1), txtname)
    
        Set rfoundcell = Columns(1).Find(What:=txtname, After:=rfoundcell, _
            LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False)
    
    Next lcount
    
    For lcount = 1 To WorksheetFunction.CountIf(Columns(1), txtchange)
    
        Set rfoundcell = Columns(1).Find(What:=txtchange, After:=rfoundcell, _
            LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False)
    
    Next lcount
    
    For lcount = 1 To WorksheetFunction.CountIf(Columns(1), txtdate)
    
        Set rfoundcell = Columns(1).Find(What:=txtdate, After:=rfoundcell, _
            LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False)
    
    Next lcount
    
    Exit Sub
    ErrorHandler:
    MsgBox MyMsg, MyType, MyTitle
    
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: search form in excel range only

    For lcount = 1 To WorksheetFunction.CountIf(Columns(1), txtname)

    Set rfoundcell = Columns(1).Find(What:=txtname, After:=rfoundcell, _
    LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False)

    Next lcount
    this loop will only retain the last found value for rfoundcell, maybe you should be creating a non-contiguous range for each found cell using Union, then you can search that range with the next search parameter
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    Re: search form in excel range only

    Hey Westconn1 thanks for your reply, sorry it took me a while to get back to you.

    I tried a few new things and i got it to work for searching within ranges only, but now it only finds one result, where as previously it would find more then one, like say "john smith and jane smith" it would only find john. and stop there. can you see where im going wrong?

    heres the new code:

    Code:
    Private Sub Cmd_OK_Click()
    Dim MyText As String
    Dim MyMsg As String
    Dim MyType
    Dim MyTitle As String
    
    MyText = txtname.Value
    MyText = TxtChange.Value
    MyMsg = "No matches were found."
    MyType = vbOKOnly + vbExclamation
    MyTitle = "No Matches"
    
    On Error GoTo ErrorHandler
    
        Application.Goto Reference:="Name"
            Selection.Find(What:=txtname, After:=ActiveCell, LookIn:=xlFormulas, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False).Activate
    
    If txtname.Value = "" Then
    
        Application.Goto Reference:="CO"
            Selection.Find(What:=TxtChange, After:=ActiveCell, LookIn:=xlFormulas, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False).Activate
    
    If TxtChange.Value = "" Then
    
    Range("A1").End(xlDown).Select
    
    GoTo finish
    
    Exit Sub
    ErrorHandler:
    MsgBox MyMsg, MyType, MyTitle
    finish:
    
        MsgBox ("Enter something dummy!")
    End If
    End If
    End Sub

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: search form in excel range only

    I don't see where you are looping any more. What would make it find more than one result?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    Re: search form in excel range only

    Thats the point, it doesn't. i only managed to get it to search in ranges only, but it only finds one result, usually the first on in the list.

  6. #6
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: search form in excel range only

    In your original post, you had a For loop.

    If you take it out, you'll have to put something else in its place (like a While...FindNext is not nothing, or something like that).

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    Re: search form in excel range only

    Got it working excellent many thanks for your help Westconn1 & vbfbryce!!

Posting Permissions

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



Click Here to Expand Forum to Full Width