尝试运行宏时出现以下错误:
Run-time error '1004':
Application-defined or object-defined error
这是宏:
Sub multiFindNReplace()
Dim myList, myRange
Set myList = Sheets("Receivable").Range("CN2:CO6") 'two column range where find/replace pairs are
Set myRange = Sheets("Receivable").Range("A2:BQ1000") 'range to be searched
For Each cel In myList.Columns(1).Cells
myRange.Replace What:=cel.Value, Replacement:=cel.Offset(0, 1).Value, _
LookAt:=xlWhole, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
Next cel
End Sub
当我点击' debug'时,突出显示为黄色的3行是:
myRange.Replace What:=cel.Value, Replacement:=cel.Offset(0, 1).Value, LookAt:=xlWhole, _
MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
我该怎么做才能解决这个问题?
答案 0 :(得分:0)
我怀疑myList
myRange
第1列中的值
您可以在尝试替换不存在的内容之前对此进行测试,如果找不到则继续前进。见下文。 (我删除了SearchFormat
个参数,因为它们似乎不适用于此处(默认值很好))
Option Explicit
Sub multiFindNReplace()
Dim myList as Range, myRange as Range, cel as Range
Set myList = Sheets("Receivable").Range("CN2:CO6") 'two column range where find/replace pairs are
Set myRange = Sheets("Receivable").Range("A2:BQ1000") 'range to be searched
For Each cel In myList.Columns(1).Cells
Dim rngFound as Range
Set rngFound = myRange.Find(cel.Value, LookAt:=xlWhole, MatchCase:=True)
If not rngFound is Nothing Then
myRange.Replace What:=cel.Value, Replacement:=cel.Offset(0, 1).Value, _
LookAt:=xlWhole, MatchCase:=True
End If
Next cel
End Sub