Schleifenende bei FindNext
30.04.2017 18:03:50
Gerd
An folgendem Beispiel eine Frage zur Code-Struktur:
Private Sub CommandButton1_Click()
Dim WS As Worksheet: Set WS = ActiveSheet
Dim sfErg As Range
Dim eingabe As String
If TextBox1 = "" Then
MsgBox "Bitte Suchbegriff eingeben!"
Else
eingabe = TextBox1.Value
With WS.Range("A:A")
Set sfErg = .Find(eingabe, LookIn:=xlValues, lookat:=xlWhole)
If Not sfErg Is Nothing Then
firstAddress = sfErg.Address
Do
If MsgBox("" & eingabe & " wurde gefunden in Zelle: " _
& sfErg.Address(0, 0), vbInformation + vbOKCancel, _
"Wert wurde gefunden in: ") = vbCancel Then Exit Do
Set sfErg = .FindNext(sfErg)
Loop While Not sfErg Is Nothing And sfErg.Address firstAddress
End If
End With
End If
End Sub
Weshalb wird hier im Schleifenende auf "Not sfErg Is Nothing" geprüft,
obwohl die Schleife doch (Debug.Errors ausgenommen) immer zur "firstAddress" zurück läuft?
Gruß Gerd