使用.FindNext方法在VBA中进行无限循环调试

使用.FindNext方法在VBA中进行无限循环调试,vba,excel,Vba,Excel,我正在学习VBA,在使用.FindNext方法时遇到了一个循环。我尝试了很多方法来修复循环,但无论我做什么,我最终都会陷入一个无限循环,你知道当你在excel中编码时,这是多么烦人。请,如果有人可以修复我的代码将是一个很大的帮助 Private Sub cbGO_Click() Dim ws As Worksheet, OutputWs As Worksheet Dim rFound As Range Dim strName As String Dim coun

我正在学习VBA,在使用.FindNext方法时遇到了一个循环。我尝试了很多方法来修复循环,但无论我做什么,我最终都会陷入一个无限循环,你知道当你在excel中编码时,这是多么烦人。请,如果有人可以修复我的代码将是一个很大的帮助

Private Sub cbGO_Click()

    Dim ws As Worksheet, OutputWs As Worksheet
    Dim rFound As Range
    Dim strName As String
    Dim count As Long, LastRow As Long
    Dim IsValueFound As Boolean


    IsValueFound = False
    Set OutputWs = Worksheets("Summary")    '---->change the sheet name as required
    LastRow = OutputWs.Cells(Rows.count, "A").End(xlUp).Row

    On Error Resume Next
    strName = ComboBox1.Value
    If strName = "" Then Exit Sub
    For Each ws In Worksheets

        If ws.Name <> "Lists" And ws.Name <> "Summary" Then

            With ws.UsedRange

                Set rFound = .Find(What:=strName, LookAt:=xlWhole)
                If Not rFound Is Nothing Then
                    firstAddress = rFound.Address

                    Do

                    rFound.EntireRow.Cells(1, "B").Resize(1, 4).Copy
                    OutputWs.Cells(LastRow + 1, 1).PasteSpecial xlPasteAll
                    Application.CutCopyMode = False
                    LastRow = LastRow + 1
                    Set rFound = .FindNext(rFound)

                    Loop While Not rFound Is Nothing And rFound.Address <> fristAddress

                End If
            End With
        End If
    Next ws
    On Error GoTo 0
    If IsValueFound Then
       OutputWs.Select
       MsgBox "Result pasted to Sheet Output"
    Else
        MsgBox "Value not found"
    End If

End Sub
Private Sub-cbGO\u Click()
将ws设置为工作表,将ws输出为工作表
暗光范围
将strName设置为字符串
按长度计算,最后一行按长度计算
Dim IsValueFound为布尔值
IsValueFound=False
设置OutputWs=工作表(“摘要”)--->根据需要更改工作表名称
LastRow=OutputWs.Cells(Rows.count,“A”).End(xlUp).Row
出错时继续下一步
strName=ComboBox1.Value
如果strName=“”,则退出Sub
对于工作表中的每个ws
如果ws.Name“列表”和ws.Name“摘要”,那么
使用ws.UsedRange
Set rFound=.Find(What:=strName,LookAt:=xlWhole)
如果没有找到,那就什么都不是了
firstAddress=rFound.Address
做
rFound.EntireRow.Cells(1,“B”).调整大小(1,4).复制
OutputWs.Cells(LastRow+1,1).PasteSpecial xlPasteAll
Application.CutCopyMode=False
LastRow=LastRow+1
设置rFound=.FindNext(rFound)
未找到时循环为Nothing And rFound.Address fristAddress
如果结束
以
如果结束
下一个ws
错误转到0
如果找到了,那么
OutputWs.Select
MsgBox“结果粘贴到图纸输出”
其他的
MsgBox“未找到值”
如果结束
端接头
您需要写“firstAddress”,而不是“fristAddress”。:D

Not rFound时循环为Nothing And rFound.Address fristAddress
在修正了这个错误之后,它应该可以工作了

另一个注意事项:您尚未将IsValueFound设置为True


编辑:哦,好的,其他人发现它的速度更快:)

拼写错误
fristAddress
vs
firstAddress
。(使用
选项Explicit
并声明所有变量以避免此类错误)击败我!第二,还要加上一些括号,以确保分组/优先级不是和,而是您真正想要的。但我怀疑是拼写错误造成的。对不起,我不明白。我宣布穿礼服,但仍然不起作用。任何人都可以通过编写代码来检查来回答这个问题吗?@Ralph,我确信没有拼写错误。特别是如果我对第一个地址所在的行进行注释,请尝试将ComboBox1.Value更改为ComboBox1.Text
Loop While Not rFound Is Nothing And rFound.Address <> fristAddress