Replace text or leave it alone pls.

  • Working on a code to replace specific text with other text but it is clearing all cells even if its not in the list.


    How would I tell it to leave the cells alone if the and values in place if the change doesn't apply?


    For example - If I am telling it to replace HORSE with COW, it is also removing the DOG.


    The particular part of the code I believe is the culprit and that I am unable to figure out is -


    [TABLE="width: 326"]

    [tr]


    [td]

    For Each c In rngR

    [/td]


    [/tr]


    [tr]


    [td]

    If Not c.Value = "" Then curVal = c.Value

    [/td]


    [/tr]


    [tr]


    [td]

    With rng

    [/td]


    [/tr]


    [tr]


    [td]

    .Replace curVal, c.Offset(0, 2).Value, xlWhole, , True

    [/td]


    [/tr]


    [/TABLE]

  • Re: Replace text or leave it alone pls.


    With this line of code:

    Code
    If Not c.Value = "" Then curVal = c.Value


    You are essentially saying: If a cell in rngR has a value in it then replace whatever values in Rng have curVal in it with c.Offset(0,2).Value


    Is this what you want to do? If not what do you expect it to do? Can you give a before and after example or post a sample workbook with non-sensitive information?

    Matt Mickle
    Using Excel 2010,2013 & 2016

Participate now!

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