+ Reply to Thread
Results 1 to 43 of 43

Search Within String

  1. #1
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Search Within String

    Im a complete newbie, got help with the code below, but I need to configure it to find the second space with the string.

    Please Login or Register  to view this content.
    I need a code for the location of the second Space...

    Thanks.
    Last edited by [email protected]; 01-26-2012 at 12:15 PM.

  2. #2
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Search Within String

    Perhaps you want the Split function
    Please Login or Register  to view this content.
    Good luck.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Search Within String

    Why use code when there are a couple of perfectly straightforward Excel functions to achieve this. viz.

    Please Login or Register  to view this content.
    If you really do want to do this with code then just use the Application.WorksheetFunction equivalent, e.g.

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Unfortunately I still have the same issue,.. not getting past the first space. after inserting your code.

    Please Login or Register  to view this content.
    Last edited by [email protected]; 01-10-2012 at 10:58 AM.

  5. #5
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    So heres the issue.. Im trying to move words around withing a text cell. Ive been successful with the word one and word two, I need to put the remaining words in the right order automatically.

    Please Login or Register  to view this content.
    Im stuck on the third word.

  6. #6
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Search Within String

    Perhaps you misunderstood my point - using Split you get an array of each item in the string broken up on the spaces. So
    Please Login or Register  to view this content.
    will return an array holding
    AAA - item 0
    GGG - item 1
    FFF - item 2
    EEE - item 3
    BBB - item 4
    DDD - item 5
    CCC - item 6

    which you can manipulate as you desire. To put the array back together again when you have finished, use
    Please Login or Register  to view this content.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Search Within String

    Hi,

    Your original post was to do with searching within a string.
    This latest post seems to be suggesting a different requirement. Is it?

    I suspect you may be better off using standard excel functions to handle this. What's the maximum number of words in a string and exactly how are you wanting to manipulate them. Reverse them or some other rule?

    You'd be better off uploading the workbook and showing a few example results and explain how you arrive at the results if it's not blindingly obvious. We can then decide the appropriate way of handling your request.

  8. #8
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Thanks Alot Richard,

    Ok heres the deal from scratch.

    I have thousands of descriptions to cleansed.

    I have created 7 columns with various keywords that are in these description so its just a matter of arranging them appropriately.

    The macro loops through all the columns, looking through each cell to find if a word in the keyword and modifier columns exist in the dirty description., if it does it then arranges them as follows.

    Keyword, 1st modifier, 2nd Modifier, 3rd modifier,.. etc.

    And that is what has brought me to this macro.

    Please find attached my prototype file.

    Thanks.
    Attached Files Attached Files

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Search Within String

    Hi,

    Are you taking each word in columns B:H on a particular row and searching the WHOLE of column K to find ANY of these words, or just searching within the column K cell on the same row as the words being searched for.

    Can you add some typical results, covering all permutations you may encounter so that we can see a before and after position.

    Regards

  10. #10
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Thanks alot Richard,

    if you would copy and paste the following into cell K2 and then run the macro, you will better understand what im talking about,..


    FFF CCC EEE DDD BBB GGG AAA


    The macro arranges the words AAA and BBB , i want it to perform the action on all the other words.


    The macro goes through each cell in Colum H to find if any word exist within the cell at also exist in column B, if that is the case, it takes the word and put it in the first position in the H cell, it goes on the cell C to do the same but this time puts it in the second word position within the cell in column H and then again goes on to do the same for column D making the result apper as the third word with the Cell in column H and on and on like that,.. so if all works well,. after the macro is ran every cell in column H should assume this naming nomenclature:

    Keyword 1st modifier 2nd modifier 3rd modifier 4th modifier 5th modifier 6th modifier

    e.g. AAA BBB CCC DDD EEE FFF GGG



    Thanks.

  11. #11
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Search Within String

    Hi,

    Can You clarify please. You mention column H but this is merely the 6th modifier column. Is this a typo and you meant column K.

    Putting the string you mention in K2 and running the macro does indeed re-order the words - or at least the first two - I note that the others need checking and sorting in as well. But putting the same string in K3 and running the macro also re-orders it yet none of the words in B3:H3 appear in the string.

    I originally thought that you were checking each of the words in the given string in column K and then if they exist on the same row in cols B:H you were reordering them as a new value in column K. It seems that I'm mistaken.

    Can you therefore describe exactly what you want to do and add some typical results, explaining if it's not blindingly obvious how you get to them.

    Regards

  12. #12
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    OMG Im sorry I meant column K... H was a typo.


    Thanks.

  13. #13
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Has anyone found a way out for me? I would really appreciate your much needed help..

    Thanks

  14. #14
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Search Within String

    Possibly, if you can respond to my last para in post # 11

  15. #15
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    I'll take it from the top...

    I have a macro that searches by looping through cells in column K, see if a word in each of the cells exist in columns B:H

    if this is true, the words are arranged in the order of the columns.

    B,C,D,E,F,G,H.

    for example:

    col B col C col D col E col F col G col H
    AAA BBB CCC DDD EEE FFF GGG
    SAM TOY PUK ROK TIT POP LOC


    So if we have a dirty description, FFF BBB GGG AAA CCC DDD EEE in cell K2,

    after the macro runs, K2 becomes AAA BBB CCC DDD EEE FFF GGG



    I have been successful up till the third word. I cant seem to get the code right to make it go into the right position.

    Please Login or Register  to view this content.
    the bold red line is where im having issues, i want to find the location of the second "space" in the entire string.

  16. #16
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    I'll take it from the top...

    I have a macro that searches by looping through cells in column K, see if a word in each of the cells exist in columns B:H

    if this is true, the words are arranged in the order of the columns.

    B,C,D,E,F,G,H.

    for example:

    col B col C col D col E col F col G col H
    AAA BBB CCC DDD EEE FFF GGG
    SAM TOY PUK ROK TIT POP LOC


    So if we have a dirty description, FFF BBB GGG AAA CCC DDD EEE in cell K2,

    after the macro runs, K2 becomes AAA BBB CCC DDD EEE FFF GGG



    I have been successful up till the third word. I cant seem to get the code right to make it go into the right position.

    Please Login or Register  to view this content.
    the bold red line is where im having issues, i want to find the location of the second "space" in the entire string.

  17. #17
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    x = InStr(2, sTmp, " ") 'Return 4, location of the first space leftPart = Mid(sTmp, 1, x) 'Return AAA<space>

    rightPart = Mid(Replace(sTmp, sKey, ""), x)

  18. #18
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Search Within String

    Perhaps this is close?
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    This is not just close!!!

    This is IT!!!!!!

    OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOH my God,.. it feels like im dreaming!!!! Oh my!!!! Im screaming out so loud right now you cant even imagine,.. You are simply a genius!!! THANK YOU!!!!!!!!!!!!!!!!!!!! it works like magic!!!!! Thanks alot bro!!!

    Thank You!!!!!!!!!!!!!!!!!!!!!!

  20. #20
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Search Within String

    You are most welcome.

  21. #21
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    [QUOTE=OnErrorGoto0;2679667]You are most welcome.[/QUOTE


    Aagain thanks alot @OnErrorGoto0 ,

    I observed though that in alot of cases the leading keywords were removed from the string after the macro ran. I attach an example. Please run the module 2 macro. you will observe what im talking about.. any leads as to how I can correct this?

    Notice Column M: which represents what the coreect results shoule be.

    Thanks again.
    Attached Files Attached Files

  22. #22
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Search Within String

    Oh, I think I see the issue (it's searching for "" and matching blank cells). Amended version
    Please Login or Register  to view this content.
    which I hope will rectify that.

  23. #23
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Thanks alot.... It totally worked!!!

    Thanks so much.!

  24. #24
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Thanks alots again...

    Ok so,.. after several hours of trying to manipulate your code to solve another glitch I encountered,.. Ive been unsuccessful at it. can you please open the attchment and not the line in yellow before and after you run the macro..

    I really appreciate your effort, honestly, ..

    Thanks

    Quote Originally Posted by OnErrorGoto0 View Post
    Oh, I think I see the issue (it's searching for "" and matching blank cells). Amended version
    Please Login or Register  to view this content.
    which I hope will rectify that.
    Attached Files Attached Files

  25. #25
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Search Within String

    Apologies - that was just sloppiness on my part
    Please Login or Register  to view this content.

  26. #26
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Thank you sooo much,..You have done so much already,.. God bless you!!

  27. #27
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Search Within String

    Maybe ?
    Please Login or Register  to view this content.



  28. #28
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    People Im stuck again,.. unfortunately @onerrorgoto Im stiull experience the last glitch.. If you run the macro in module 3, you will find out that some descriptions lost some words, while some picked up some words,..

    How can we do it to make the macro just move the words around without deleting anything from the cell and without adding words that are not originally within the dirty description. just arranging according to the modifiers..

    Please compare the column J, K, L after the macro in module 3 is ran.

    Its so so sad that I have to keep coming back for help,.. But i feel so stuck and frustrated,. Ill really appreciate your help,.

    Thanks
    Attached Files Attached Files

  29. #29
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Search Within String

    First of all you should take care that all values that have to be looked up are in the range B2:H100.

    novalue isn't nor disposable, etc

    If you change the worksheet, the macro has to adapted too.
    Base on you last example:

    Please Login or Register  to view this content.

  30. #30
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Any value within the string that is not found in the reference column should not be deleted but moved to the back of the descriction as the last words,..

    and also columns B:H are dynamic, I update it daily and it so it cant be limited.,...



    Quote Originally Posted by snb View Post
    First of all you should take care that all values that have to be looked up are in the range B2:H100.

    novalue isn't nor disposable, etc

    If you change the worksheet, the macro has to adapted too.
    Base on you last example:

    Please Login or Register  to view this content.

  31. #31
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Search Within String

    Re the last 2 entries, BONE and REPAIR both appear in the 2nd Modifier column - what should the code do? For the others it appears to be doing what it should, since some of the words do not match (e.g. REUSEABLE in the text but REUSABLE in the modifiers.)

  32. #32
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Any value within the string that is not found in the reference column should not be deleted but moved to the back of the descriction as the last words,..

    If 2 words exist in the same columns, it should pick the first one it finds as the modifier,..

    Thanks alot for replying,..

    Quote Originally Posted by OnErrorGoto0 View Post
    Re the last 2 entries, BONE and REPAIR both appear in the 2nd Modifier column - what should the code do? For the others it appears to be doing what it should, since some of the words do not match (e.g. REUSEABLE in the text but REUSABLE in the modifiers.)

  33. #33
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Search Within String

    So it should not use the same column twice? Currently the code is replacing the previous entry with the next one found.

  34. #34
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    No it should not,...

    Example:
    FILTER MEMBRANE STERILE DISPOSABLE APC

    once it finds the leading Keyword in the first column or the first modifier or second,...etc,.., it should not look in that columns again for each particular cell, it moves to the next, if the word is not found in any column, it is pushed to the back of the other words..


    No additional words should be added from other descriptions and no words should be deleted from each description...


    Thanks alot again for your interest../

  35. #35
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Search Within String

    Also, based on what you are saying, BONE in the last two will not get matched, since REPAIR will be matched with column D first, and BONE only appears in column D.
    As for the words being added, that is because you have not used the last version of the code I posted, so you are missing a crucial line. For what you have said, this should work
    Please Login or Register  to view this content.

  36. #36
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    No it should not,...

    Example:
    RETREIVER STANDARD ROTH NET 2.5MM 230CM 5/BX NOVALUE


    once it finds the leading Keyword in the first column or the first modifier or second,...etc,.., it should not look in that columns again for each particular cell, it moves to the next, if the word is not found in any column, it is pushed to the back of the other words..(E.G NOVALUE in the example cant be found in the reference columns, which is why it is at the back)


    No additional words should be added from other descriptions and no words should be deleted from each description...


    Thanks alot again for your interest../


    Quote Originally Posted by OnErrorGoto0 View Post
    So it should not use the same column twice? Currently the code is replacing the previous entry with the next one found.

  37. #37
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Search Within String

    I think you'd better match your description to the example in the workbook.
    After each answer you introduce a new requirement.
    Please analyse your requirements beforehand.

  38. #38
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Looks good,.. At this point I dont believe theres any way possible to achieve the perfect result for this task,.... Ill still have to so some of the lines manually,... but is there a way I can colour the cell whose ALL of its words have been found in the reference columns and arranged in the Description cell; GREEN and then colour the once whose words were not found or some of it not found or has one glitch or the order, Colour red? is that possible by any chance?


    Quote Originally Posted by OnErrorGoto0 View Post
    Also, based on what you are saying, BONE in the last two will not get matched, since REPAIR will be matched with column D first, and BONE only appears in column D.
    As for the words being added, that is because you have not used the last version of the code I posted, so you are missing a crucial line. For what you have said, this should work
    Please Login or Register  to view this content.

  39. #39
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Im Sorry but I keep realizing these glitches after each run,.. its not possible for me to foresee all the problems before hand,..

    Thanks so much for your keen interest,..

    Quote Originally Posted by snb View Post
    I think you'd better match your description to the example in the workbook.
    After each answer you introduce a new requirement.
    Please analyse your requirements beforehand.

  40. #40
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Search Within String

    Please do not quote: see the forum rules

  41. #41
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Search Within String

    This will do the colouring too
    Please Login or Register  to view this content.

  42. #42
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Thank you so much Onerrorgoto,.. you have been so helpful..

    Many regards,...

  43. #43
    Registered User
    Join Date
    12-30-2011
    Location
    London, Ont
    MS-Off Ver
    Excel 2007
    Posts
    69

    Re: Search Within String

    Onerrorgoto,.yeah so this is what Im talking about,.. can you plssssssssssssssssss take a look at workbook,Will it be possible for you to help me see if these rules be implemented in the macro... this is in an effort to make the program smarter,..

    Its your code actually but I have modified it to do more stuff...


    1. There should an action before the cleaning, this will look through column I and the cells in column K, if a word exist in column I and is found anywhere in Colum K, this word should be deleted from column K.

    2. The leading Keyword Column B can Have to words in one cell,.. so if any of those two words are found in the same description in a cell in Column K, they will appear as found in Colum B and occupy the first position in the cleaned description.


    Then 3rdly,..for the following words the actions next to it should be followed,..



    IF FOUND IN DIRTY DESCRIPTION......... GENERALLY MEANS

    % (or "PERCENT")....................Starts with "SOLUTION"
    FLAVOUR................................Starts with "SUPPLEMENT"
    OIL.......................................Starts with "LUBRICANT"
    LEFT, RIGHT.........................Starts with "INSERT" OR "IMPLANT"
    FEEDING...............................Starts with "TUBE"
    POWDER FREE......................Starts with "GLOVE"
    EXAM................................Starts with "GLOVE"
    ROLL..................................Starts with "TAPE" or "BANDAGE" or "DRESSING"
    STRETCH ...........................Starts with "BANDAGE" or "DRESSING" or "SLEEVE"
    ALKALINE .......................... Starts with "BATTERY"
    LITHIUM............................Starts with "BATTERY"
    BLOOD PRESSURE.................Start with "CUFF"
    SELF SEAL..........................Starts with "POUCH"
    ANTI-SKID..........................starts with "COVER SHOE"


    The work book::: PROTOTYPEE.xlsm



    Thank you sooooooooooooooooooooooooooooooo much bro,.

    Dee

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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