【发布时间】:2021-09-26 19:59:56
【问题描述】:
这是我在运行宏时收到的错误。我想我已经解决了当 find 函数通过实现 if 语句找不到我正在搜索的内容时。不知怎的,它仍然无法正常工作......
如果我正在搜索的值在我的记录中不存在,我该如何跳过查找/替换语句?
我的 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
【问题讨论】:
-
替换值是否会产生无效的公式?
-
错误出现在哪一行???
-
顺便说一句 -
FormulaVersion是Range.Replace的新参数,不出所料,documentation 尚未更新 :-) -
它发生在第一个 Selection.Replace(示例一)上。 @BigBen 这是什么意思?同样不,它不应该像以前那样创建无效的公式。只是现在我在另一个文件上使用了它,它找不到值,因此给出了错误
-
其实是的,FormulaVersion 似乎导致了这个问题。