Because you don’t provide examples of how your macro is misbehaving it’s hard to give sensible advice. For example, examples of the clipboard content before/after and the search/replace strings that demonstrate the problem.
In the meantime, you might want to look at this thread which might provide some insight: