Alright, I'm stumped. I have a large worksheet with rows upon rows of names, some have (UPY) after them, most don't. I need to remove the (UPY). To make things even more fun, between the name and the (UPY), some of them have 1 space, some 2 spaces, and some 3 spaces. I got the following macro to work on most of the culprits, but some are not working and I cannot, for the life of me, figure out why. My only guess at this point is hidden formatting that I've never encountered and therefore haven't checked.
Also, doing a manual Find and Replace (or just a Find for that matter) works when searching just for (UPY), but not when you include the spaces. I do need those spaces gone because I am actually referring to this sheet in another workbook, so it has to match. If I had to guess again, based on this information, something is going on with those spaces. Even when I copy and paste values, theoretically removing any and all formatting, no change.
Any ideas?
Sub RemoveUPY()
mystring = " (UPY)"
Set RangeObj = Cells.Find(What:=mystring, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If RangeObj Is Nothing Then MsgBox "Not Found" Else RangeObj.Activate
Cells.Replace What:=" *(UPY)", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
mystring2 = " (UPY)"
Set RangeObj = Cells.Find(What:=mystring2, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If RangeObj Is Nothing Then MsgBox "Not Found" Else RangeObj.Activate
Cells.Replace What:=" *(UPY)", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
mystring3 = " (UPY)"
Set RangeObj = Cells.Find(What:=mystring3, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If RangeObj Is Nothing Then MsgBox "Not Found" Else RangeObj.Activate
Cells.Replace What:=" *(UPY)", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
The attached document includes the macro and one cell that the macro works on (B1), and one cell that it does not work on (A2).
test.xlsm
Bookmarks