Looking closer at the original version, I don't see how it could have worked at all. The last condition (not name == "...") will always be true because name is not defined at this point, leading to an empty target.
A simple fix for your macro would be replacing "break" with "return". I've just tested it with my house keeper and it worked fine, so I guess it will work with cattles, too. Also the last part of the macro could be changed to a simple TargetUnit("") without any conditions, but let's do things step by step.