+ Reply to Thread
Results 1 to 40 of 40

vba code : find exact phrase in Column C using Column M and replace with Column N

  1. #1
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    vba code : find exact phrase in Column C using Column M and replace with Column N

    i have this macro that is close, but has an issue.
    for each row of C, M, N i need this to happen:
    when the Exact Complete Phrase in Column M is found in Column C, then the Complete Phrase in Column N replaces it.
    the issue i'm having is there are Phrases in Column C and Column M which are similar or one is just an extra word or numbers longer than the other.

    please see the sample below with the different Chevrolet Silverado Truck Models. it is not working correct because it needs to do each one at a time for the exact phrase.
    it's not working when you have: "Silverado 1500" "Silverado 1500 Series" "Silverado 1500 HD" etc....these should all be change to: "Silverado"


    here is the code i'm using:
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by daveexcelforum; 03-28-2021 at 02:20 PM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Not sure if this is what you are after:

    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor
    Join Date
    09-30-2018
    Location
    Vlaams Brabant Belgium
    MS-Off Ver
    365
    Posts
    456

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Please Login or Register  to view this content.
    your range was from row 2 and your list was counted on column 2 which should be 13 or "M"
    Please be as complete as possible in your asking so it may save use all the time to rework the solution because you didn't give all the requirements. If you have a layout in mind please work it out first so we can adapt our solution to it. Thanks.
    If you have been helped, maybe you could click the *

  5. #5
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    The total list of Model Names to change is 193.
    The macro works except it doesnt work when you have example:
    "Silverado 1500" "Silverado 1500 Series" "Silverado 1500 HD" etc....these should all be changed to: "Silverado" but that's where the problem is.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Try reordering the list like this:

    HTML Code: 
    Last edited by TMS; 03-28-2021 at 02:45 PM.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  7. #7
    Valued Forum Contributor
    Join Date
    09-30-2018
    Location
    Vlaams Brabant Belgium
    MS-Off Ver
    365
    Posts
    456

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    You should look for te longest string first because if the short string is found it will change that first and leave the "series" or "HD"

  8. #8
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Ok, I can try these ideas
    I was hoping there was some additional code that would fix it without changing order.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    I was hoping there was some additional code that would fix it without changing order.
    Why would you hope that?

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    This is the reason:
    You should look for te longest string first because if the short string is found it will change that first and leave the "series" or "HD"
    Doesn't really matter what the code does if you look at the shortest string first ... it wrecks the rest of the searches.

    The only other option, I think, would be to use Text to Columns to split the entries then use xlWhole to make the changes. Then Join them up again. Seems like a lot of work when all you have to do is sort the list of replacements.

  11. #11
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Pl try this code. It is working ok.
    sheet1(2) is having data after running macro.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by kvsrinivasamurthy; 03-29-2021 at 02:14 AM.
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  12. #12
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    kvsrinivasamurthy - your code did not work.

    TMS - when i sorted the data z to a it worked better but still doesn't work

    Still having issue with the "Truck Model Names" and a few others.

    I've attached the full list of names to change.

    I'm thinking the model names change as it goes down the list....but then a later change causes a conflict with a row which has already been changed.
    Also, each model name change ends up changing thousands of rows of data in column C....and also many times it is 2 or more times in each row/cell of column c.
    Attached Files Attached Files

  13. #13
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    As per example Post #1 attachment, you could try this
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    jolivanes....you're the man!
    so far everything i'm checking looks good.

    if i want to run this same thing on column B (2)
    what do i change in your code?

    thanks again.

  15. #15
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Please Login or Register  to view this content.
    should work for the same setup.

  16. #16
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Don't know why the other suggestions don't work as you mentioned because a find and replace like this, which is just about the same, works.
    Try it on your actual workbook, or a copy would be better, and let us know.
    Please Login or Register  to view this content.

  17. #17
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    I don't see how any code will work if it uses the list as presented.

    Just looking at the first part of the list ...

    Silverado 1500
    Silverado 1500 Series
    Silverado 1500 HD
    Silverado 1500 Series 4x2
    Silverado 1500 Series 4x4

    all to be replaced with Silverado. The problem is that, wherever Silverado 1500 occurs, regardless of whether or not there are other descriptors, it will be replaced by Silverado. Hence, Silverado 1500 Series will be replaced by just Silverado Series on the first pass. Subsequently, Silverado 1500 Series is no longer present to be replaced with just Silverado and is left as Silverado Series. And the same applies to the rest of the 1500 entries.

    Splitting and using xlWhole and recombining is the only mechanism that will work ... unless you sort the list descending.

  18. #18
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    You're right Trevor. My mistake. I did not check it close enough. It leaves part of the name.

  19. #19
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    hi jolivanes,

    i tried changing the one line in your code as you described, changing the "3" to "2".
    it did not work.
    is there something else in the code that needs to change?

    here is what the data is like in column 2 (b):

    <B> Applications: GMC Sierra 1500 Series 2007-2013; GMC Yukon 2007-2014; GMC Yukon XL 2007-2014; GMC Sierra Denali 2007-2010; GMC Sierra Denali 1500 2011-2013; GMC Savana 1500 2010-2013</B>

  20. #20
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Attach a representative workbook.

  21. #21
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    I thought maybe it was a format issue in column 2, but still didn't work.
    The sheet is the same, column 2 has data like this:

    <B> Applications: GMC Sierra 1500 Series 2007-2013; GMC Yukon 2007-2014; GMC Yukon XL 2007-2014; GMC Sierra Denali 2007-2010; GMC Sierra Denali 1500 2011-2013; GMC Savana 1500 2010-2013</B>

    When the macro runs for column 3, it takes about 15 seconds to complete and works properly.

    When I change the one line to 2, it doesnt go into that processing mode and doesnt do anything.

    Thanks again.

  22. #22
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    It should not do anything.
    One has a pipe delimiter while this one has a semicolon delimiter.
    Twice change the pipe delimiter ("|") in the code to a semicolon delimiter (";")
    Please Login or Register  to view this content.
    becomes
    Please Login or Register  to view this content.
    and
    Please Login or Register  to view this content.
    becomes
    Please Login or Register  to view this content.
    Last edited by jolivanes; 03-30-2021 at 04:35 PM.

  23. #23
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Hello jolivanes,
    ";" didn't work.
    do you think it has something to do with the <B> and </B> ?

    thanks again

  24. #24
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Attach a workbook with the original data in it.
    If there is a lot of data in it, delete some so the size of the workbook is reasonable.

  25. #25
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    row 2 contents:

    <B> Applications: GMC Sierra 2500 Series 2002-2002; GMC Sierra C3 2001-2003; GMC Sierra Denali 2004-2006; GMC Yukon 2001-2006; GMC Yukon XL 2001-2006; GMC Yukon XL Denali 2004-2006</B> <BR>Size: 17x7.5 <BR>Lugs: 6 <BR>Bolt Pattern: 5.5in <BR>
    Attached Files Attached Files

  26. #26
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    There is data both in Column B and C but no replacement values anywhere.

  27. #27
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    the replacements are in M and N but much higher in the sheet as it's only 193 rows of data.
    i'll send another with some sample data in M and N

  28. #28
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    attached the sample again with data in m and n
    Attached Files Attached Files

  29. #29
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Have you tried using it on Column 3 (=Column C)?
    The data in Column C is obscuring the data in Column B so even if it did change the data in Column B, you would not see it because of that.
    Or clear all data in Column C and see if your code works on Column B

  30. #30
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    It's not that.
    For some reason it just wont run on column b (2).
    I've changed the 3's to 2's
    Also tried changing the | to ;

  31. #31
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Both "<B> Applications: " at the beginning and "</B> <BR>Size: 17x7.5 <BR>Lugs: 6 <BR>Bolt Pattern: 5.5in <BR>" ought to be removed.
    You can do that with code or manually.
    There is no "Yukon XL Denali" in Row B3. There is "GMC Yukon XL Denali 2004-2006".
    BTW. Do you have to pay for each line published? Just one little line is a bit cheap isn't it.

  32. #32
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Dang, it's always something isn't it.

    Thanks for taking a run at it.

  33. #33
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Well, what do you want to do?
    Can we delete the parts that need deleting?
    We should do it in an understandable way so you can change it for future similar problems.

  34. #34
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    I can't permanently delete the <b> and <br> stuff.
    If there is a script that temporarily deletes it or ignores it to properly change the Model Names that would be ok .
    As long as they are back after running it.

  35. #35
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    jolivanes,

    it doesn't need to be 1 macro to do both column B (2) and C (3) at the same time.

    you're code above in response #13 works for column C(3)

    columns M(13) and N(14) remain the same.

    just somehow need to make the exact same Model Name changes to column B(2) as what we do to column C(3)

  36. #36
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Is "GMC Yukon XL Denali 2004-2006" the same for changing purpose as "Yukon XL Denali"?

  37. #37
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    The to be replaced values and the replacement values are in Columns M and N starting at M1.
    The data to be checked is in Column B starting at B3.
    I suggest to try on a copy of your original workbook first.
    Don't know if it does what needs to happen because an attachment with just one row is pretty skimpy.
    Please Login or Register  to view this content.
    For future threads.
    Attach a representative workbook with between 10 and 20 examples, not just 1.

  38. #38
    Registered User
    Join Date
    03-28-2021
    Location
    michigan
    MS-Off Ver
    office 2010
    Posts
    16

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Hi jolivanes,

    getting a "runtime error "S" invalid procedure call or argument"

    dbug is highlighting this: dd = Mid(c, InStr(Len(d), c, "<"))

  39. #39
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    Attach a representative workbook with at least 20 rows of data and multiple rows for the data to look for and to change into (Columns M and N)
    If there are different strings at the beginning and at the end, make sure that shows. If they are the same on all, also let us know.

  40. #40
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: vba code : find exact phrase in Column C using Column M and replace with Column N

    See attached
    Attached Files Attached Files

+ 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: 4
    Last Post: 04-27-2020, 09:04 AM
  2. Replies: 3
    Last Post: 11-25-2014, 06:08 AM
  3. Please help !!!!Formula to replace the Phrase of column A in Column B
    By Bangera in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-14-2014, 12:00 PM
  4. [SOLVED] Need help with VB/Macro code for multiple find and replace within one single column
    By The Saw in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-17-2014, 01:26 PM
  5. [SOLVED] Find Value in 1 column based on another column then replace with value of yet another.
    By johanbosman in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-07-2013, 11:50 AM
  6. Have a column return a phrase based on a number in another column
    By luke.guthrie in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2012, 04:04 PM
  7. Replies: 2
    Last Post: 02-27-2011, 11:59 AM

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