View Single Post
 
Old 10-14-2020, 06:21 AM
pushpi004 pushpi004 is offline Windows 10 Office 2016
Novice
 
Join Date: Feb 2020
Posts: 21
pushpi004 is on a distinguished road
Default Solved: >Case sensitivity not followed when using replace method

Hi all..
Looks I have come across a solution to my problem. It is indeed using MatchCase as suggested by John, but in a slightly more developed logic. Nevertheless, it does the trick.

Sub DoReplace(tr As TextRange, findThis, replaceWith)
If InStr(1, tr.Text, findThis, vbTextCompare) > 0 Then 'is the word found at all?
'found at least one case form - just try to replace any case form found...
tr.Replace findWhat:=LCase(findThis), Replacewhat:=LCase(replaceWith), _
WholeWords:=True, MatchCase:=True
tr.Replace findWhat:=UCase(findThis), Replacewhat:=UCase(replaceWith), _
WholeWords:=True, MatchCase:=True
tr.Replace findWhat:=StrConv(findThis, vbProperCase), _
Replacewhat:=StrConv(replaceWith, vbProperCase), _
WholeWords:=True, MatchCase:=True
End If
End Sub

I hope it helps others as well.
Best of luck!
Cheers!
Reply With Quote