2017-03-20 5 views
-1

私は、特定のテキストを見つけて、次にそのセルを選択し、次にテキストを見つけて置き換えるために、以下の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 

答えて

0

Sub test() 
    Dim LastRow As Long 
    Dim LastCol As Long 
    Dim rgn As Range   

    With ThisWorkbook.Sheets("Sheet1") 
     LastRow = .Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row 
     LastCol = .Cells.Find("*", SearchOrder:=xlByColumns, LookIn:=xlValues, SearchDirection:=xlPrevious).Column 
     Set rgn = .Range(.Cells(1, 1), .Cells(LastRow, LastCol)) 
    End With 

    For Each cell In rgn 
     cell.FormulaR1C1 = Replace(cell.Value, "example", "newtext") 
    Next 
End Sub 

は、あなたがの名前で「シート1」を交換する必要があることにご注意くださいあなたのスプレッドシート。また、スプレッドシートが空の場合、コードはランタイムエラーを生成します。

0

あなたはちょうどあなたが使用することができ、シート全体で相互に1つのテキスト文字列を置き換えることを見ている場合:

Sub ReplaceString() 

    ThisWorkbook.Worksheets("Sheet1").Cells.Replace _ 
     What:="STRING", Replacement:="", LookAt:=xlPart, MatchCase:=False 

End Sub 

私はあなたが値を見つけようとしているので、あなたがエラーを取得している理由だと思いますその値のセルを選択します。見つからない場合はエラーが発生します。これはセルを選択する前に確認する必要があります。

これは変数を使用して、見つかった範囲を格納します。範囲を選択する前に、または範囲で何かを行う前に、範囲が何もないことを確認するチェックが行われます。

Sub Test() 

    Dim MyFoundRange As Range 

    Set MyFoundRange = ThisWorkbook.Worksheets("Sheet1").Cells.Find(what:="STRING", After:=ActiveCell, LookIn:=xlFormulas, lookat _ 
     :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
     False, SearchFormat:=False) 

    If Not MyFoundRange Is Nothing Then 
     MyFoundRange.Select 
    End If 

End Sub 
0
Sub Test2()  
    With ActiveSheet '<--| or, if you want to specify a given sheet, use 'With Worksheets("MyGivenSheetName")' 
     Intersect(.UsedRange, .Columns(1)).Replace What:="NAME", Replacement:="ALTERNATIVENAME", LookAt:=xlPart, MatchCase:=False '<--| act on used cells only to avoid processing all column A cells 
    End With 
End Sub