mardi 26 avril 2016

If value in cell look in next column and replace it VBA

I am using this function:

Public Function replaceWord(t As String)
   For Each c In Columns(1).SpecialCells(2)
   t = Replace(t, c.Value, c.Offset(0, 1).Value)
   Next
   replaceWord = t
End Function

to replace values.

Like this:

enter image description here

So the function work like this, it checks if values in column C are also in column A. If found it, is going to replace it with values in column B.

I would like to adjust this function so only if exact values in column A are found then do the replacement. Like in the picture, where text in column C becomes text in column E, so "a b abaaa", as the value"abaaa" is not present in column A.

At the present the function does this, which is wrong, as value "abaaa" should not be replaced it:

enter image description here

Hope this is clear enough!

Aucun commentaire:

Enregistrer un commentaire