Search Range For Part Strings

  • Hi,


    I am using the following code to search a database of information and then display it on a different sheet.


    The user types the search term into a textbox and then presses a command button to search the database.


    Currently it only searches for an exact match. How can i adapt it so it searches for similar strings?


    Thanks


  • Re: Search Database For Similar Strings


    Quote from StephenR

    How do you define "similar"? I would imagine using Find would be a better method.


    If i enter "ab" in the text box i want it to return all rows with "ab" in any of the cells. So it would return aberdeen, abstract, pablo etc. Any cells with ab anywhere in them would be returned.


    How would i use the find method? Is there not a way i can get it to look for part of a string rather than an exact match with the code i already have.


    Thanks for your help.

  • Re: Search Range For Part Strings


    Quote from Dave Hawley

    rossfourfive, I'll lower your assumed experience as using FIND is basic at best.


    Mate, I appreciate you have to enforce your forum rules strictly but i really struggle to understand why this has to lead to the extremely patronising tone you use in almost every single post.


    I can use find by following the guide you have written on it which is great but i don't want to use find for this. Is there no way i can adapt the code i am currently using to search for part strings? I know its possible to use *s to search for part strings but can it be used in conjunction with the textbox i am using?


    Once again, thank you for your help.

  • Re: Search Range For Part Strings


    Because you had rated yourself above those answering YOUR question.


    Quote

    I can use find by following the guide you have written on it which is great but i don't want to use find for this.

    Why?

  • Re: Search Range For Part Strings


    Quote from Dave Hawley

    Because you had rated yourself above those answering YOUR question.


    Why?


    I would consider my knowledge of excel to be above average. My knowledge of VBA however is basic. I apologise for the confusion.


    Is there a way to adapt my code to search for a part string or is the only way to achieve this to re-write the code to use find instead?


    If i am to use find how do i adapt -


    Code
    Cells.Find(What:="Cat", After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False).Activate


    - to use the textbox as the search string and to print the results of the search on another sheet as i have done in my example in OP. Also which bit of the code makes it search for part strings?


    Thanks

  • Re: Search Range For Part Strings


    Hi rossfourfive


    the find is a in-built function very fast al the guns first choice but


    try Like by playing with

    Code
    If rCell = Sheet2.TextBox1 Then


    to something similar to

    Code
    rCell like "*" & Sheet2.TextBox1 & "*"


    untested but the module help is self explanatory


    hope it helps
    pike

  • Re: Search Range For Part Strings


    Try this. It's untested so if it doesn't work, please post a sample workbook.

  • Re: Search Range For Part Strings


    I think instr might work - have a look at the help file, the syntax is pretty straightforward and you seem to know what you're doing. Get back to us if that doesn't help.


    PS, sometimes I too find the great DH can OVERUSE the bold and italics to make you feel a bit WORTHLESS - remember he moderates about a thousand moron posts a day (I don't mean to include you with that) and has helped thousands of people via this forum

  • Re: Search Range For Part Strings


    Pike,


    I have played around with

    Code
    rCell Like "*" & Sheet2.TextBox1 & "*"


    and used

    Code
    cellcheck = rCell Like "*" & Sheet5.TextBox1 & "*"
            If cellcheck = True Then


    This does find part strings but i'm having a problem with it repeating rows and not clearing the results before each search. I will have another look at this if we can't get the find to work how i need it to. Thanks for the tip though.


    Stephen,


    I pasted your code into the workbook and couldn't get it to work. Played around with it a little but still no luck so i have attached a sample book as requested. Cheers for having a look at this.


    Charlie,


    Haven't had time to check this out but will look at it later, thanks for that.

  • Re: Search Range For Part Strings


    rossfourfive mate
    try...






    works for me


    regards pike


    :sheep:

  • Re: Search Range For Part Strings


    Pike,


    Thats working now, thanks a lot. One more thing...how would i change it so that the case doesn't matter in the search?


    Thats one advantage of the find method Stephen is using...


    Stephen,


    If i type in "Glasgow" it finds them but not for part strings. So if i type "Glas" nothing happens. I'm keen to get this find method working properly as it seems to be the more efficient way of doing it.

  • Re: Search Range For Part Strings


    OK, yes I see - have now amended CountIf line:

  • Re: Search Range For Part Strings


    Quote

    I would consider my knowledge of excel to be above average

    Then how come you don't that FIND can search for part cell values and can be case sensitive or not?


    I see you are NOW using the Find code, when you had convinced yourself it was no good :)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!