有人可以在下面的代码中指出我哪里出错了。 我需要在我使用find命令的范围内找到所有出现的值。为了开始寻找下一个事件,我再次使用find命令但是在第一个找到的单元格之后。然而,用于调试目的的消息框显示第二个find命令也指的是同一个第一个找到的单元,因为这两个命令都具有与输出相同的单元地址。
有人可以指出这是什么错误。我已经花了一天时间,但我仍然无法弄清楚错误。
提前致谢。
Set FoundCell = Range("Act_No").Find(what:=PreActivityArray(i))
If Not FoundCell Is Nothing Then
firstaddress = FoundCell.Address
Do
MsgBox (CStr(FoundCell) & " address " & CStr(FoundCell.Address) & " " & CStr(FoundCell.Row))
Set FoundCell = Range("Act_No").Find(what:=PreActivityArray(i), after:=FoundCell)
MsgBox (CStr(FoundCell) & "address " & CStr(FoundCell.Address) & " " & CStr(FoundCell.Row))
Loop While Not FoundCell Is Nothing And FoundCell.Address <> firstaddress
End If
答案 0 :(得分:2)
改为使用FindNext
方法:
Set FoundCell = Range("Act_No").Find(What:=PreActivityArray(i))
If Not FoundCell Is Nothing Then
firstaddress = FoundCell.Address
Do
MsgBox FoundCell & " address " & FoundCell.Address & " " & FoundCell.Row
Set FoundCell = Range("Act_No").FindNext(FoundCell)
If FoundCell Is Nothing Then Exit Do
If FoundCell.Address = firstaddress Then Exit Do
MsgBox "Another instance of value: " & FoundCell.Address
Loop While True
End If
还要注意,我正在使用If FoundCell Is Nothing Then Exit Do
您不能像原始代码一样使用行Loop While Not FoundCell Is Nothing And FoundCell.Address <> firstaddress
,因为如果FoundCell
为Nothing
,则FoundCell.Address
会触发运行时错误。
您也可以阅读: .Find and .FindNext In Excel VBA