How to Loop a VBS Script

Written by alexander gokhfeld
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Loop a VBS Script
VBS scripts allow users to add additional functions to Windows applications. (Comstock/Comstock/Getty Images)

Microsoft Visual Basic Script (VBS) language allows users to add additional functions to their Window applications. It also gives you the ability to loop the execution of VBS script when you need to run a group of VBS statements repeatedly. You can repeat the statements until a condition is true or false or repeat statements a specific number of times. The loop statements in VBS scripts help you to add additional options to Windows applications.

Skill level:


  1. 1

    Click "Start' in Windows followed by "Microsoft Visual Basic Script Editor."

  2. 2

    Enter the following code in the open window:

    Sub ChkFirstWhile()

    Dim counter, myNum

    counter = 0

    myNum = 20

    Do While myNum > 10

      myNum = myNum - 1
      counter = counter + 1


    MsgBox "The loop made ' & counter & ' repetitions."

    End Sub

    Sub DoMyProc50Times()

    Dim x

    For x = 1 To 50



    MsgBox "The loop repeats 50 times."

    End Sub

    Sub cmdChange_OnClick

    Dim d 'Create a variable

    Set d = CreateObject("Scripting.Dictionary")

    d.Add "0", "Greece" 'Add some keys and items

    d.Add "1", "France"

    d.Add "2", "Spain"

    For Each I in d

      Document.frmForm.Elements(I).Value = D.Item(I)


    MsgBox "The loop repeats ' & d & ' times."

    End Sub

  3. 3

    Click "File" followed by "Save' to apply the code VBS script in your application.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.