Fandom

Visual Basic Wiki

For

66pages on
this wiki
Add New Page
Talk0 Share

For LoopEdit

A for loop is used when you need to repeat code a certain amount of times. It also can be used in objects like collections.

For To LoopsEdit

Basic UseEdit

The for loop is structured like this

For i = 1 To 20
    MsgBox "I have annoyed you " & i & " times! :D"
Next i

It could be shown like this

i = 1
While i <= 20
    MsgBox "I have annoyed you " & i & " times! :D"
    i = i + 1
Wend
  • i is the variable that is used.
  • 1 is the start value to set
  • 20 is the value that it will attempt to get to.

Advanced UseEdit

For i = 20 To 1 Step -1
     MsgBox "I will annoy you " & i & " more times! Mwuhahaha!"
Next i
  • The Step statement can be used to skip a certain amount of times instead of 1, so -1 decreases the value

For Each LoopsEdit

For Each vVariant In cCollection
   MsgBox vVariant
Next

It means vVariant will be set to each item in the collection. This works only with loop counters that are of Variant or of any object type (either Object or something more specific).

NotesEdit

  • In VBScript, you are required to use "Next" and not "Next i" etc.
Flow Control
If | For | While | Do | Until

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.