I have an Excel VBA program that loops through each row of data in a data sheet.
My goal is to exit the while loop once boolean bFound
is set as True
.
I think my condition "Or bFound=True" might be incorrect.
bFound = False
While Sheets("Data").Cells(iRow, 1) <> "" Or bFound = True
If Sheets("Data").Cells(iRow, 11) = Sheets("Data2").Cells(iRow, 1) Then
bFound = True
End If
iRow = iRow + 1
Wend
'exit loop after the boolean=true
Using Do While or Do Until allows you to stop execution of the loop using Exit Do instead of using trickery with your loop condition to maintain the While ... Wend syntax. I would recommend using that instead.
Exit Do Loop We can exit any Do loop by using the Exit Do statement.
VBA/VBScript Differences VBA supports the End statement, which immediately terminates execution of code and, in the case of Visual Basic, terminates the application.
In visual basic, we can exit or terminate the execution of the while loop immediately by using Exit keyword. Following is the example of using Exit keyword in a while loop to terminate loop execution in a visual basic programming language.
Use Do ... Loop
and Exit Do
bFound = False
Do While Sheets("Data").Cells(iRow, 1) <> ""
bFound = Sheets("Data").Cells(iRow, 11) = Sheets("Data2").Cells(iRow, 1)
If bFound Then Exit Do
iRow = iRow + 1
Loop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With