+ Reply to Thread
Results 1 to 2 of 2

Code to search Excel and return results even if part of search text is present

  1. #1
    Registered User
    Join Date
    08-30-2016
    Location
    Harare, Zimbabwe
    MS-Off Ver
    2010
    Posts
    5

    Code to search Excel and return results even if part of search text is present

    Hello guys!

    I am an excel VBA newbie and I have created an excel workbook that I use to store meeting minutes. As part of the application I have a userform from which I can search any previously saved data. below is a snippet of the code I am using:

    Dim d As Range

    With Worksheets("Bank").Range("C2 : C10000")

    Set c = .Find(SearchTextBox.Text, LookIn:=xlValues)
    Set k = Worksheets("Bank").Columns("C").Find(what:=SearchTextBox.Text, LookIn:=xlValues, lookat:=xlWhole)
    If Not c Is Nothing Then
    On Error GoTo ErrMsg
    DisplayUserform.TextBoxResults1.Text = Sheets("Bank").Range("A" & k.Row)
    DisplayUserform.TextBoxResults2.Text = Sheets("Bank").Range("B" & k.Row)
    DisplayUserform.TextBoxResults3.Text = Sheets("Bank").Range("C" & k.Row)
    DisplayUserform.Show
    Worksheets("Bank").Activate
    Range("A" & k.Row).Activate
    Worksheets("Default").Activate

    End If

    If c Is Nothing Then
    On Error GoTo ErrMsg
    DisplayUserform.Show
    DisplayUserform.TextBoxResults1.Text = "---"
    DisplayUserform.TextBoxResults2.Text = "---"
    DisplayUserform.TextBoxResults3.Text = "Sorry, your search did not return any results."
    Worksheets("Default").Activate

    End If
    End With
    Exit Sub


    The problem is that with this code I can only return results if the search query exactly matches the saved data. How do I search and return results if the search query is part of the saved data? For example I would like a situation where the saved data is "I love code" and if I search for "code" or just "I love" I will still be able to return results.

    May someone please help me as soon as you can.

    Thanks vey much code gurus!!!

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Code to search Excel and return results even if part of search text is present

    Hi Taoyuan00,

    Please review the forum rules, especially #3 regarding code tags when posting code. But... try changing

    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    also add ", lookat:=xlPart" to your "set c...." statement
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

+ 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. Replies: 2
    Last Post: 05-14-2015, 01:30 PM
  2. Replies: 6
    Last Post: 08-22-2014, 08:47 AM
  3. VBA code - Search box function - return value/text not location
    By kruwo1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-15-2014, 01:55 AM
  4. VBA Code - Search text & search number & search qty and result - Urgent Please
    By naresh73 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-15-2014, 10:51 AM
  5. [SOLVED] search for specific text in a file and return a value to a cell based on the results
    By JJGF in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-03-2013, 07:00 PM
  6. Replies: 3
    Last Post: 07-27-2011, 02:29 PM

Tags for this Thread

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