April 05, 2020

VBScript tutorial 7 | Arrays

This is the next tutorial in the VBScript Tutorials for Beginners. This VBScript beginner tutorial explains the VBScript array variable. Please view the VBScript tutorial 7 or read on... What is array in VBScript? An array variable in VBScript can store multiple values in it. Such data values may be customer names or phone numbers or email addresses etc. A VBScript array has indexes to refer the different values in it. The indexes start with the index 0. In the VBScript example below, the Sub FixedArray shows an array example, strCustomers(3). We can run this VBScript in the Command Prompt using the CScript  command e.g. CScript Array1.vbs

' VBScript code
Option Explicit
Call FixedArray
Call DynamicArray

Sub FixedArray
    ' Declare a fixed array i.e. an array with the specified number of elements.
    Dim strCustomers(3)
    strCustomers(0) = "Abe"
    strCustomers(1) = "Ben"
    strCustomers(2) = "Chris"
    strCustomers(3) = "Dustin"
    ' Display the first data value in the command window, instead of a message box.
    WScript.Echo "strCustomers(0) is " &  strCustomers(0)
End Sub

Sub DynamicArray
    ' Declare a dynamic array i.e. an array whose number of elements is unknown at present.
    Dim strCustomersNew()
    ' VBScript Redim statement defines the number of elements in the array.
    Redim strCustomersNew(3)
    strCustomersNew(0) = "Abe"
    strCustomersNew(1) = "Ben"
    strCustomersNew(2) = "Chris"
    strCustomersNew(3) = "Dustin"
    ' Preserve in the Redim statement retains the existing array elements.
    Redim Preserve strCustomersNew(5)
    strCustomersNew(4) = "Eddie"
    strCustomersNew(5) = "Fred"
    Dim i
    ' VBScript UBound function gives the upper bound of the array.
    For i = 0 to UBound(strCustomersNew)
        WScript.Echo "The element" &  i & " is " &  strCustomersNew(i)
    Next
End Sub

Next, let us see the VBScript code with an array of numbers.

March 29, 2020

VBScript tutorial 6 | While Wend | Do While Loop | Working with Excel

Moving on to the next tutorial in the VBScript Tutorials for Beginners. This VBScript beginner tutorial explains the VBScript While Wend loop and the VBScript Do While loop. It also gives the VBScript code to create an Excel file. Please view the VBScript tutorial 6 or read on... A While Wend statement in VBScript is a conditional statement. It is useful when we don't know the number of times a code block should run. The While Wend loop runs a code block while a condition is true. Let us see VBScript While Wend example.

' VBScript code
Option Explicit
Dim i
' Initialize the counter variable, i
i = 1
Loop1

Sub Loop1
While i<= 5
    MsgBox "The current value of i is " & i, vbInformation, "While loop"
    ' Increment the value of i
    i = i + 1
Wend
End Sub

A Do While statement in VBScript is also a conditional statement. Like the While Wend statement, it is useful when we don't know the number of times a code block should run. The VBScript Do loop has two variations. The first variation of the VBScript Do While Loop checks if a condition is true. If the condition is true, it runs a code block. A VBScript Do While Loop statement example is in the Sub Loop1 below. The second variation of the VBScript Do Loop While runs a code block, and then checks if a condition is true. A VBScript Do Loop While statement example is in the Sub Loop2 below. Even if the condition is false, the Do Loop While statement runs the code block once (because it checks the condition after running the code block).

' VBScript code
Option Explicit
Dim i
Loop1
Loop2

Sub Loop1
i = 1
Do While i<=5
    MsgBox "The current value of i is " & i, vbInformation, "Do While Loop"
    i = i + 1
Loop
End Sub

Sub Loop2
i = 1
Do
    MsgBox "The current value of i is " & i, vbInformation, "Do Loop While"
    i = i + 1
Loop While i <= 5
End Sub

Next, let us see the VBScript code to create an Excel file. This VBScript uses the VBScript With statement that allows us to specify an object. The benefit is that we don't have to repeat that object's name on every line of code between the With statement and the End With statement.

March 22, 2020

VBScript tutorial 5 | For Next Statement

This is the next tutorial in the VBScript Tutorials for Beginners. This VBScript beginner tutorial explains a VBScript conditional statement, the VBScript For Next statement, which is a loop statement in VBScript. Please view the VBScript tutorial 5 or read on... What is loop in VBScript? A loop is a code block that can run repeatedly, depending on a condition. For Next loop in VBScript runs a fixed number of times. Let us see VBScript for loop examples.

'VBScript code
Option Explicit
Dim i
Call Loop1
Call Loop2
Call Loop3

Sub Loop1
For i = 1 to 5
    ' All statement(s) between For and Next statements are run for each value of i from 1 to 5.
    MsgBox "The current value of i is " & i, vbInformation, "For Loop1"
Next
End Sub

Sub Loop2
For i = 1 to 10 Step 2
    ' The statement(s) are run for the values of i from 1 to 10, increasing by 2 every time.
    MsgBox "The current value of i is " & i, vbInformation, "For Loop2"
    ' Uncomment the following If Then statement to exit the For loop when i is 5.
    'If i = 5 Then Exit For
Next
End Sub

Sub Loop3
For i = 10 to 1 Step -2
    ' The value of decreases from 10 to 1, by 2 every time.
    MsgBox "The current value of i is " & i, vbInformation, "For Loop3"
Next
End Sub

Next, let us learn about the For Each loop in VBScript. It runs a code block on each value in an array or a collection. Let us see a VBScript for each loop example. This VBScript code lists the files present in the given folder.