A Do/Loop loop is used for repeating something for infinity, or until a condition is true. It loops certain code so that depending on the conditions, when it reaches the end of the loop, it will restart the loop and repeat the code.


Here is the layout for the loop.

     Code to loop goes here

The Do function defines the start of the loop. Anything in between the Do and the Loop will get repeated. Here's an example of looping a message box until they click no, using a variable with the name "CheckMessage".

Do Until CheckMessage = vbNo
     CheckMessage = MsgBox("Click no to close this. Click yes for it to come again.", vbYesNo)

That uses the "Until" condition - it says that it will loop the code until CheckMessage equals vbNo (They click no). Here's a loop using the While function as a conditional statement, using a variable with the name "CheckMessage".

Do While CheckMessage = "False"
     CheckMessage = MsgBox("Click no to close this. Click yes for it to come again.", vbYesNo)
     If CheckMessage = vbYes Then
          CheckMessage = "True"
          CheckMessage = "False"
     End If

Your code will repeat for infinity until you shut it down with task manager or some such thing if you just leave it as the next example, so be careful:

     MsgBox "Haha"

You want to avoid that sort of thing unless you want to be annoying. However, one way out of a loop like that would be to use an Exit Do function. Here's an example:

     If MsgBox("Click no to close this.", vbYesNo) = vbNo Then
          Exit Do
     End If

The Exit Do will bring the code to the end of the loop and continue with the next line after "Loop".




If Statement

Remember, if you ever need help, you can ask BusyCityGuy on his talk page.

Did you find what you needed?

The poll was created at 23:16 on June 10, 2012, and so far 71 people voted.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.