+ Reply to Thread
Results 1 to 8 of 8

Find & Replace from list

  1. #1
    Registered User
    Join Date
    09-18-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    35

    Question Find & Replace from list

    Hi there,

    I've attached an example spreadsheet to show more clearly what I am trying to do...

    In sheet "strings" column A I have several text strings that contain location names, such as:

    puppies in new york
    jobs in chicago
    real estate in boston
    apartments in chicago
    cars for sale in new york

    In sheet "locations" I have a list of locations:

    Boston
    Chicago
    New York
    Washington

    I am trying to find a way of searching through the text strings, if one of the locations exists in the string then it is replaced with the text "location". The final output should look like this:

    puppies in location
    jobs in location
    real estate in location
    apartments in location
    cars for sale in location

    Unfortunately I'll need to do this on a far bigger scale than the attached example, therefore I can't just do a basic Excel Find & Replace job. I've tried searching on Google with no luck, I've also tried to use the =SEARCH & REPLACE features but this has only worked when I do one location at a time.

    Any help, advice or pointers in the right direction would be greatly appreciated.

    Thank you in advance.

    ChainsawDR
    Attached Files Attached Files
    Last edited by ChainsawDR; 08-27-2010 at 09:25 AM.

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Find & Replace from list

    This code should get you started.

    Adjust the ranges as required, and note that it is case specific, so watch your capitalisation!.

    Please Login or Register  to view this content.
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Registered User
    Join Date
    09-18-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: Find & Replace from list

    Thank you Sweep. The macro failed the debugger as it was missing a " but thats sorted now, however I can't get it working...

    I've added it, ran it, tried running the macro while selecting the cells in the "Strings" sheet but no joy.

    How should it work? Thank you again Sweep.

    I've attached "Example2" which shows how I've implemented the Macro (in case it's something I've done wrong).
    Attached Files Attached Files
    Last edited by ChainsawDR; 08-26-2010 at 08:41 AM. Reason: typos

  4. #4
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Find & Replace from list

    In your example, the place names in the second sheet are capitalised, by not in the first sheet, therefore, they're not a match.

  5. #5
    Registered User
    Join Date
    09-18-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: Find & Replace from list

    that would be why! thanks, and sorry, you did already point out the capitalisation.

    That is awesome - I was worried for a moment that it only worked when the location name was at the end of the string, but it works for "New York Cars" as well as "cars in New York".

    If anyone uses anything similar in future, it stopped working after running the macro the first time. I added "location" to the list of locations and it worked again (as it was replacing "location" with "location").

    Sweep, you have my thanks - really appreciated (and sorry for the capitalisation brain fart).

    Cheers

    ChainsawDR

  6. #6
    Registered User
    Join Date
    09-18-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: Find & Replace from list

    Hi Sweep,

    I've come across a few obstacles... I think I've got over all but one and was hoping you could advise please.

    Two of the locations are "Manchester" & "Chester". Because the string chester is contained within manchester it often produces an output like this: "manlocation". This is just one example, quite often a location name can be spelt within a 'normal' search term.

    Therefore I think I need to include Regular expressions within the code, so that rather than looking for the string anywhere, it looks like this:

    ^manchester$|^manchester_|_manchester_|_manchester$

    this should ensure the problem detailed above is avoided. Is this possible to do within the code?

    Due to another problem I have already added a RegEx module (http://www.tmehta.com/regexp/add_code.htm) so there are are no longer any non-normal characters in the list of strings, and I have also changed all spaces to underscores. The code runs fine, it just needs the regex adding and then its complete.

    I did think of sorting the "Location" sheet so that longer location names were searched first (i.e. manchester is replaced before chester) but this still leaves the problem of small 3/4 letter location names appearing within a normal, non-location word.

    Thanks again for all your help so far Sweep - no problem if Regex isn't your area.

    ChainsawDR
    Last edited by ChainsawDR; 08-26-2010 at 11:55 AM. Reason: extra info

  7. #7
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Find & Replace from list

    Hi,

    modified to change the text only if the searched text is at the start of a cell, or following a space.

    Please Login or Register  to view this content.
    Apologies, I have little knowledge of Regex

  8. #8
    Registered User
    Join Date
    09-18-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: Find & Replace from list

    sweep I am in your debt. The code didn't quite work as it stood as it left a few possibilities open for error, but I've changed the code so its now covered:


    Please Login or Register  to view this content.
    Thank you so much for your help Sweep - I've got everything working and have learnt quite a bit more VBA.

    Thanks again

    ChainsawDR

+ 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