+ Reply to Thread
Results 1 to 6 of 6

vba Replace function not finding string

  1. #1
    Registered User
    Join Date
    05-25-2016
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    3

    vba Replace function not finding string

    I have a very weird problem in trying to use the Replace function on the body of a template email. In the email I have a number of special phrases that I replace programatically in vba. e.g. "The captain is zzPlayerNamezz, and he will blah blah". I use the replace function to find and replace the zzPlayerNamezz in a kind of mail merge. This generally works fine but is sometimes very odd. For example If I try to replace "zzPlayerNamezz" it works fine but if try to replace "zzPlayerNamezz," it cannot find it.

    the string is "According to our records your child, zzPlayerNamezz, is currently playing..."

    the code is :
    With MailItem
    .htmlbody = Replace(.htmlbody, "zzPlayerNamezz,", "")

    If iwere to use .htmlbody = Replace(.htmlbody, "zzPlayerNamezz", "") it works fine


    what am I doing wrong??

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: vba Replace function not finding string

    May be there is extra space after the string "zzPlayerNamezz" and before the comma like that "zzPlayerNamezz ," and this may cause that
    In fact it would be correct using this line
    Please Login or Register  to view this content.
    as I tested something similar and worked for me well
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  3. #3
    Registered User
    Join Date
    05-25-2016
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    3
    Quote Originally Posted by YasserKhalil View Post
    May be there is extra space after the string "zzPlayerNamezz" and before the comma like that "zzPlayerNamezz ," and this may cause that
    In fact it would be correct using this line
    Please Login or Register  to view this content.
    as I tested something similar and worked for me well
    Thanks for the thought but there are no blanks. If I take the comma out it finds the string without the comma but if I put it in it does not find it

  4. #4
    Registered User
    Join Date
    05-25-2016
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    3

    Re: vba Replace function not finding string

    I finally figured it out if anyone is reading this thread. The string I was checking was HTML code from an email template.
    When I used the code zzPlayerNamezz it would show up as a spelling error when viewing the email template and was saved as such.
    In the HTML code there was an inserted </span> code at the end of the spelling mistake.
    If there was no comma after it then all was fine, however if there was a comma after the "zz" then the</span> code was inserted between the "zz" and the ",".
    Hence the string was never found. Spellcheck the template ignore the errors so they no longer highlight and the problem goes away!

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: vba Replace function not finding string

    Thanks for the feedback. Glad you could figure it out
    Regards

  6. #6
    Registered User
    Join Date
    11-04-2019
    Location
    Canada
    MS-Off Ver
    10
    Posts
    1

    Re: vba Replace function not finding string

    Quote Originally Posted by noblerot View Post
    I finally figured it out if anyone is reading this thread. The string I was checking was HTML code from an email template.
    When I used the code zzPlayerNamezz it would show up as a spelling error when viewing the email template and was saved as such.
    In the HTML code there was an inserted </span> code at the end of the spelling mistake.
    If there was no comma after it then all was fine, however if there was a comma after the "zz" then the</span> code was inserted between the "zz" and the ",".
    Hence the string was never found. Spellcheck the template ignore the errors so they no longer highlight and the problem goes away!
    THANK YOU!! I had the exact same problem and finally came across your comment to spell check (I added the terms to dictionary). You rock!!

+ 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. Function or Formula to replace a dynamic string with a NULL value
    By sryme in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 08-31-2016, 11:23 AM
  2. [SOLVED] Need help with REPLACE function to replace HEX A0 with HEX 20 in string
    By flyboy54 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-26-2013, 12:52 PM
  3. Replace variables in string and perform function
    By Jefferson in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-02-2012, 07:53 PM
  4. Replies: 15
    Last Post: 07-05-2012, 12:49 PM
  5. Finding a line in file and replace string
    By gombi in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-30-2011, 12:32 AM
  6. Instr Function - Replace String 2 with Variables?
    By max57 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-08-2010, 03:14 PM
  7. [SOLVED] function for finding position of numeric character in a string
    By Paul in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-05-2005, 10:29 PM

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