How to Loop a VBS Script

Save

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.

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

  • 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

    Loop

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

    End Sub

    Sub DoMyProc50Times()

    Dim x

    For x = 1 To 50

      MyProc

    Next

    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)

    Next

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

    End Sub

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

References

  • Photo Credit Comstock/Comstock/Getty Images
Promoted By Zergnet

Comments

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!