VBA 中的查找函数不是查找下一个
我正在尝试写一个宏。我所在的部分是我的参考 ID 可以在一列中多次找到。它位于文本框中,但单元格只会有一次参考 ID。该参考 ID 最多可以匹配多行。我的宏的其余部分工作正常。当我尝试使用 find 或 findnext 时,我就搞砸了。我有一个计数器,用于查找我的参考 ID 在我分配给范围变量的列范围 I:I 中出现的次数。我在 do while 循环中从该计数器中减去并在其中使用 find next 。问题是在找到前两个唯一值后,它只会卡在找到的第二个值上并继续填充该值。这是我到目前为止的代码。
If Counter > 0 Then
'Uses cellFound as a range set to our original find
Set cellFound = DidIFind.Find(valueToSearch)
'Keeps going till we find all the values that the counter ticks down
Do While Counter > 0
Set cellFound = cellFound.FindNext
Set cellFound = DidIFind.Find(valueToSearch)
'Marks row position of new duplicate find so we can add to ID string
Rw = cellFound.Row
ConcatString = ConcatString & ", " & lookUpSheet.Cells(Rw, Col).Value
'Counter ticks down by 1
Counter = Counter - 1
Loop
End If
I am trying to write a macro. I am at the part where my reference ID can be found multiple times in a column. It is in a text box but the cell will only have the reference ID once. That reference ID can match up to multiple rows. I have the rest of the macro working fine. It's when I try to use find or findnext that I am messing up. I have a counter that looks up how many times my reference ID showed up in the column range I:I which I have assigned to a range variable. I subtract from that counter in a do while loop and use find next in it. The problem is after finding the first two unique values it just gets stuck on the second value it found and keeps populating that. This is my code so far.
If Counter > 0 Then
'Uses cellFound as a range set to our original find
Set cellFound = DidIFind.Find(valueToSearch)
'Keeps going till we find all the values that the counter ticks down
Do While Counter > 0
Set cellFound = cellFound.FindNext
Set cellFound = DidIFind.Find(valueToSearch)
'Marks row position of new duplicate find so we can add to ID string
Rw = cellFound.Row
ConcatString = ConcatString & ", " & lookUpSheet.Cells(Rw, Col).Value
'Counter ticks down by 1
Counter = Counter - 1
Loop
End If
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Do Loop
与FindNext
Do Loop
vsFindNext
下面是如何使用
Range.FindNext
的一个很好的示例。Excel 将使用之前的
Range.Find
LookIn
和LookAt
值。因此,需要对它们进行设置以避免任何形式的歧义。FindCells
我编写了这个函数来返回所有找到的单元格的并集。
使用
数据
结果
Here is a good example of how to use
Range.FindNext
.Excel will use the previous
Range.Find
LookIn
andLookAt
values. For this reason they need to be set to avoid any kind of ambiguity.FindCells
I wrote this function to return the Union of all the found cells.
Usage
Data
Result