如果在替换字符串(选择替换,查找替换)时未找到任何内容,则错误:Excel VBA

这是运行宏时收到的错误。我以为当find函数通过执行if语句找不到我要搜索的内容时,我已经解决了。不知何故它仍然无法正常工作...

如果我要搜索的值在记录中不存在,如何跳过查找/替换语句?

enter image description here

我的VBA代码:

Columns("D:D").Select
Set Example1 = Selection.Find(what:="Example One", LookIn:=xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows)
If Not Example1 Is Nothing Then
Selection.Replace what:="Example One", Replacement:= _
    "Test One A", lookat:=xlPart, SearchOrder:=xlByRows _
    , MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False, _
    FormulaVersion:=xlReplaceFormula2
End If

Set Example2 = Selection.Find(what:="Example Two ", LookIn:=xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows)
If Not Example2 Is Nothing Then
Selection.Replace what:="Example Two ", Replacement:= _
    "Test One B", lookat:=xlPart, SearchOrder:=xlByRows, MatchCase:= _
    False, SearchFormat:=False, ReplaceFormat:=False, FormulaVersion:= _
    xlReplaceFormula2
End If
评论