【发布时间】:2014-10-20 08:25:15
【问题描述】:
我正在使用这个循环来寻找值。 .Find 有效,但 findNext 无效,省略了许多值。在这里我放弃我的代码,你有什么建议吗?非常感谢!!
For Each ws In SourceWb.Worksheets
If IsNumeric(Left(ws.Name, 3)) Then
Set gCell = ws.Columns(6).Find(what:=numdoc, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, searchformat:=False)
If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then
firstAddress = gCell.Address
Do
repetidos = repetidos + 1
finalcell = gCell.Address
'merged cells code here not displayed
oldaddress = gCell.Address
'>Having trouble here> **
Set gCell = ws.Columns(6).FindNext(after:=gCell)
'**
Loop Until gCell.Address = oldaddress
End If
End If
Next ws
【问题讨论】:
-
这对你有用吗?我收到
Next without For编译错误。 -
是的,如果在发帖时,我可能会擦除结尾,请尝试删除此代码句:If firstAddress oldaddress Then
-
你是如何声明变量的?我认为这不是问题,但我很难尝试复制您的问题。你看过
Do...Loop中的逻辑了吗? -
好的,我发现了发生了什么,我在确定的范围内进行了 .find,而我正在寻找该范围之外的其他值,这就是为什么没有找到它们的原因。所以循环和 findnext 有效,但是安排它以使其更快(我认为)的正确方法是将 .find 放在工作表之前并提取我需要的值。非常感谢您的宝贵时间:)
-
谢谢。在询问 SO 问题时,请尝试对 If 和循环结构中的缩进更加小心——这样你会得到更多的回应。我最初只发表评论是因为正确的缩进表明你只需要一个指向正确方向的指针。 祝你好运,尽情享受吧