私は、特定のテキストを見つけて、次にそのセルを選択し、次にテキストを見つけて置き換えるために、以下のVBAを使用しています。いずれの場合も、ワークシートが存在しない可能性のあるテキストをエラーを起こさずに許可したいと考えています。VBA Text Search If Exists
テキスト文字列を見つけて選択し、テキスト文字列が存在する場合にのみ検索し置換する最も良い方法は何ですか?私は、単にコードを使用するスプレッドシート全体の「をnewText」で「例」という名前のすべてのサブストリング交換するには
Sub test2()
'
' test2 Macro
' Keyboard Shortcut: Ctrl+g
Cells.Find(what:="STRING", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range("G11").Select
Cells.Find(what:="STRING", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("I13").Select
Cells.Find(what:="STRING", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Columns("A:A").Select
ActiveCell.Replace what:="NAME", Replacement:="ALTERNATIVENAME", lookat:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Find(what:="NAME", After:=ActiveCell, LookIn:=xlFormulas, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.Replace what:="NAME", Replacement:="ALTERNATIVENAME", lookat:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub