Loop While in Do...Loop Code

Dim Greeting
Dim UserName
Dim TryAgain
Do
TryAgain = "No"
UserName = "InputBox("Please enter your name:")
If Trim(UserName) = " " Then
MsgBox "You must enter your name."
TryAgain = "Yes"
Else
Greeting = "Hello. " & UserName & ", it's a pleasure to meet you."
End If
Loop While TryAgain = "Yes"
MsgBox Greeting

Do
TryAgain = "No"
UserName = "InputBox("Please enter your name:")
If Trim(UserName) = " " Then
MsgBox "You must enter your name."
TryAgain = "Yes"
Else
Greeting = "Hello. " & UserName & ", it's a pleasure to meet you."
End If
Loop While TryAgain = "Yes"
MsgBox Greeting

Now we encounter the end of our loop block. What this Loop line is essentially telling the script engine is "If the TryAgain variable equals "Yes" at this point, then go back up to Do line and execute all that code over again." If the user entered their name, then the TryAgain variable will be equal to "No". Therefore, the code will not loop again, and will continue on to the last line:MsgBox Greeting, which we've seen before.

If the user did not enter their name, then TryAgain would be equal to "Yes", which would mean that the code would jump back up to the Do line again. This is where the re-initialization of the TryAgain variable to "No" is essential. If we don't reset the value of TryAgain, then there's no way for TryAgain to ever equal anything but "Yes". If TryAgain always equals "Yes", then the loop keeps going around and around forever. This is disaster for your script.=, and for your user.

First
Next