我正在学习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
答案 0 :(得分:0)
你需要写“firstAddress”,而不是“fristAddress”。 :d
Loop While Not rFound Is Nothing And rFound.Address <> fristAddress
修正此错字后,它应该可以正常工作。
另一个注意事项:你还没有将IsValueFound设置为True。
编辑:哦,好吧,其他人更快发现它:)