VBAで文字列の置き換えを行う場合、次のようにします。
Cells.Replace What:="嫌い", Replacement:="好き", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
一般的にはこうなのですが、「正規表現が使えないなんて!」なんて人がいるかもしれません。
残念ながら、正規表現に詳しくないのでその例を示す事が出来ませんが、さわりだけ。
以前のOnePointで、MacScript関数でAppleScriptが書けると書いたのを覚えていますでしょうか?
つまり、AppleScriptを使って、正規表現やテキスト処理に強いPerlに処理を任せてしまえばよいのです。
ここでは、MacJPerlにやらせてみました。
Option Explicit
Sub Test()
Dim orgText As String
orgText = "私は、Excelが嫌い嫌い大っ嫌い!"
MsgBox ReplaseText(orgText, "嫌い", "好き")
End Sub
Function ReplaseText(iOrgText As String, iBefore As String, iAfter As String)
Dim script As String
script = "set buff to """ & iOrgText & """" & vbCr & _
"set PerlScript to ""$ans=$ARGV[0];$ans =~ s|" & iBefore & "|" & iAfter & "|g; print $ans;""" & vbCr & _
"tell application ""MacJPerl""" & vbCr & _
" set aRetList to Do Script {PerlScript, buff} mode Batch" & vbCr & _
"end tell"
Debug.Print script
ReplaseText = MacScript(script)
End Function
Debug.printでスクリプトの内容を出力していますので、イミディエイトペインで確認してみて下さい。
本当は、正規表現を駆使した例を書けるとよいのですが。。。
まあ、それはみなさんにお任せするとしますか。