【问题标题】:.FindNext in a loop doesnt' work.FindNext 循环不起作用
【发布时间】: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 和循环结构中的缩进更加小心——这样你会得到更多的回应。我最初只发表评论是因为正确的缩进表明你只需要一个指向正确方向的指针。 祝你好运,尽情享受吧

标签: excel vba loops find set


【解决方案1】:

这是我能从你的线索中得出的最好的结论:

Option Explicit

Sub Test()
Dim WS As Worksheet
Dim SourceWB As Workbook
Dim numdoc As Long
Dim gCell As Range
Dim firstAddress As String
Dim oldaddress As String
Dim finalcell As String
Dim repetidos As Long

Set SourceWB = ThisWorkbook 'added for clarity and safety
numdoc = 456
    For Each WS In SourceWB.Worksheets
        If IsNumeric(Left(WS.Name, 3)) Then 'OK I had to save it as "123 A"
            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
                Set gCell = WS.Columns(6).FindNext(after:=gCell)
                Do
                    repetidos = repetidos + 1
                    finalcell = gCell.Address
                    'merged cells code here not displayed
                    oldaddress = gCell.Address
                Loop Until gCell.Address = oldaddress
            End If
        End If
    Next WS
End Sub

不确定它是否回答了问题,但确实显示了缩进。

那里可能有With...End With 的空间,但我太累了,无法找到它。

【讨论】:

  • 可能是(和)我会寻找它并尝试将它放在 sourcewb.worksheets 中的每个 ws 之前。非常感谢你
  • 在你的结构中尽可能高地尝试它。不要忘记点.
  • 打勾就好了:)
【解决方案2】:
 This seems to work at this point.

 For Each WS In SourceWB.Worksheets
    With ws.Range("F:F")
    If IsNumeric(Left(WS.Name, 3)) Then 'OK I had to save it as "123 A"
        Set gCell = .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
            Set gCell = .FindNext(after:=gCell)
            Do


                'merged cells code here not displayed

            Loop While Not gCell Is Nothing And gCell.Address <> firstAddress
        End If
    End If
 end with
Next WS

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2019-01-23
    • 2012-03-30
    • 2011-10-19
    • 2016-03-04
    • 2012-07-02
    • 2017-04-13
    相关资源
    最近更新 更多