VBScript Tutorials: Learn VBScript From Scratch (15+ In-Depth Tutorials)

Introduction to Microsoft VBScript (Visual Basic Script): VBScript Tutorial #1

In today’s scenario, VBScript has turned out to be a very important topic, especially for beginners who wish to learn the scripting language or automation tools like QTP/UFT.

=> Click Here For The QTP Training Tutorials Series

=> SCROLL DOWN to see the complete list of 15+ In-Depth VBScript Tutorials for Beginners

We will be covering a series of VB Scripting tutorials to help the developers and testers to learn VBScript quickly in an easily understandable way. 

VBscript tutorial

In my subsequent tutorials, I will cover other important topics of VBScript like Variables, Constants, Operators, Arrays, Functions, Procedures, Excel Objects, Connections Objects, etc., which in turn will create an easy understanding among the users for learning VBScript Programming Language easily and effectively.


==> Learn VBScript with these 15 Tutorials  <==

Tutorial #1: Introduction to VBScript
Tutorial #2: Declaring and Using Variables in VBScript
Tutorial #3: Operators, Operator Precedence and Constants in VBScript
Tutorial #4: Using Conditional Statements in VBScript
Tutorial #5: Loops in VBScript  and also Part 2 here
Tutorial #6: Using Procedures and Functions in VBScript
Tutorial #7: Arrays in VBScript
Tutorial #8: Date Functions in VBScript
Tutorial #9: Working with Strings and Cookies in VBScript
Tutorial #10: Working with Events in VBScript
Tutorial #11: Working with Excel Objects in VBScript
Tutorial #12: Working with Connection Objects in VBScript
Tutorial #13: Working with Files in VBScript
Tutorial #14: Error Handling in VBScript
Tutorial #15: VBScript Interview Questions


Initially, to start with I have chosen the first topic as ‘Introduction to VBScript’.

In this tutorial, I will discuss the basics of VBScript, thereby focusing more on its features, data types supported by it and coding methodologies along with the procedure to handle comments and formats in scripts.

What is VBScript?

As the name itself explains, VBScript is a ‘Scripting Language’. It is a lightweight case insensitive programming language developed by Microsoft. It is a subset of ‘Visual Basic’ or we may also say it as a lighter version of Microsoft’s programming language Visual Basic.

Most of us would have used Visual Basic during our course curriculum in our school or college. Visual Basic is an event-driven programming language and an Integrated Development Environment from Microsoft.

VBScript language is used in QTP for coding and running Automated Test Scripts. This is not a very difficult language to learn and with a little knowledge of basic programming skills and passion for writing code, anyone can learn this easily. For those who know Visual Basic, it is an added advantage.

Automation Testers, who want to create, maintain and execute the tests in QTP need to have basic programming skills using VBScript.

Basic of VB Scripting Concepts

Now let’s move on to some basics topics that are revolving around VBScript to enable clear understanding and knowledge about VBScript.

Data types

1) There is only one data type: Variant. It can store different kinds of information based on the context in which it is used.
2) If used in a numeric context it is a number or a string if used in a string expression.
3) If a number has to behave as a string we could enclose it within “ “.
4) There are various subtypes to a variant. You can explicitly specify these subtypes to achieve a clear definition for your data. The below is a screenshot from the VB User guide that shows all the subtypes of data that can be used:
(click on image to enlarge)

VB script data types

5) Conversion functions can be used to convert one subtype of data into another.
6) Since it is the only data type available, all the return values from a function are variants.

Here are different VBScripting examples you can try on your own.


1) A variable is nothing but a space in the computer’s memory that can store certain information. This information is bound to change from time to time. Where the information goes physically is immaterial but when needed, it can be accessed or changed by addressing the name of the variable.

E.g: If there is a statement that you want to run several times, you could use a variable to contain that count. Say X. X is a variable that can be used to store, change and use the space in the memory where we want to keep the count.

2) All variables are of the datatype Variant.

3) Declaring a variable before its use is optional, although it’s a good practice to do so.

4) To make the declaration mandatory there is an “Option Explicit” Statement available. To declare variables:

Dim x – This declares  x
Dim x, y, z – This declares multiple variables
X=10 – This is how a value is assigned. As a general rule, the variable is the left-hand side component and the right is its value.
X=”Swati” – this is the way a string value is assigned.

To make declarations mandatory this is how the code has to be written:
Option Explicit
Dim x, stri

If Option explicit statement was not used, we could have directly written:
and it would not have thrown an error.

5) Naming convention: Names must start with an alphabetic character, must be unique, cannot contain an embedded period and cannot exceed 255 chars.

6) A variable containing a single value is a scalar variable and the one that has more than one is an array.

7) A one dimensional Array can be declared as Dim A(10). All the arrays in VB Script are zero-based that means the array index starts from 0 through the number declared. That means, our array A has 11 elements. Starting from 0 to 10.

8) To declare a 2-dimensional array simply separate the row count and column count by a comma. Eg: Dim A(5, 3). This means it has 6 rows and 4 columns. The first number is always row and the second a comma.

9) There is also a dynamic array whose size can change during runtime.  These arrays can be declared using dim or redim statements.

If an array is declared as Dim A(10) and during runtime, if we need more space we can do the same by using the statement: redim A(10). There is a “Preserve” statement that can be used in conjunction with the redim statement.

Dim A(10,10)
Redim preserve A(10,20)

This piece of code shows how we do it. Initially, A is a 11 by 11 array. Then we are resizing it to be an 11 by 21 array and the preserve statement will make sure that the data that is previously contained in the array is not lost.


  1. As the name implies a constant is nothing but an unchanging value in a program that is assigned a name.
  2. They can be declared by prefixing “Const” to a name.
  3. Eg: Const a=”10” or Const Astr=”Swati”.
  4. This value cannot be changed accidentally while the script is running.


Some of the important operators that are most commonly used are:

  1. String concatenation: & (Eg: Dim x=”good”&”day”, so x contains “goodday”
  2. Addition (+)
  3. Subtraction (-)
  4. Multiplication (*)
  5. Division(/)
  6. Logical negation (Not)
  7. Logical conjunction (And)
  8. Logical disjunction ( Or)
  9. Equality(=)
  10. Inequality (<>)
  11. Less than (<)
  12. Greater than(>)
  13. Less than or equal to(<=)
  14. Greater than or equal to (>=)
  15. Object equivalence(Is)

It is important to note that the list is not complete but merely a subset containing the most commonly used operators.

The operator precedence rules are:

  1. Multiplication or Division take precedence over addition or subtraction
  2. If multiplication and division exist in the same expression, then left to right order is considered
  3. If Addition and subtraction occur in the same expression, then too, left and right order is taken into consideration.
  4. The order can be overridden by using parenthesis. In this case, the expression within the parenthesis is executed first.
  5. & operator takes precedence after all arithmetic operators and before all logical operators.

Environments Supporting VBScript

Primarily, there are 3 Environments where VBScript can be run.

They include:

#1) IIS (Internet Information Server): Internet Information Server is Microsoft’s Web Server.

#2) WSH (Windows Script Host): Windows Script Host is the hosting environment of the Windows Operating System.

#3) IE (Internet Explorer): Internet Explorer is a simple hosting environment that is most frequently used to run scripts.

Data Types in VBScript

Unlike other languages, VBScript has only 1 data type called Variant.

As this is the only data type that is used in VBScript, it’s the only data type that is returned by all the functions in the VBScript.

A variant data type can contain different kinds of information, depending on how it is used. For Example, If we use this data type in String context then this will behave like a String and if we use this in the Numeric context then this will behave like a Number. This is the specialty of a Variant data type.

A Variant data type can contain several subtypes. Now, let’s take a look at what all values/data will be returned if a particular subtype is used.

Subtypes include:

#1) Empty: This subtype indicates that the value will be 0 in case of Numeric Variables and “” for String Variables.

#2) Null: This subtype indicates that there is no valid data.

#3) Boolean: This subtype indicates that the resultant value will be either true or false.

#4) Byte: This subtype exhibits that the resultant value will lie in the range between 0 to 255 i.e. the result will be from any value ranging from 0 to 255.

#5) Integer: This subtype shows that the resultant value will lie in the range between -32768 to 32767 i.e. the result will be from any value ranging from -32768 to 32767

#6) Currency: This subtype indicates that the resultant value will lie in the range between -922,337,203,685,477.5808 to 922,337,203,685,477.5807 i.e. the result will be from any value ranging from -327-922,337,203,685,477.5808 to 922,337,203,685,477.5807.

#7) Long: This subtype shows that the resultant value will lie in the range from -2,147,483,648 to 2,147,483,647 i.e. result will be from any value in between -2,147,483,648 to 2,147,483,647.

#8) Single: This subtype exhibits that the resultant value will be from any value in between -3.402823E38 to -1.401298E-45 in case of negative values.

And for positive values, the result will be from any value in between 1.401298E-45 to 3.402823E38.

#9) Double: This subtype indicates that the resultant value will be from any value in between -1.79769313486232E308 to 4.94065645841247E-324 in case of negative values.

And for positive values, the result will be from any value in between 4.94065645841247E-324 to 1.79769313486232E308.

#10) Date (Time): This subtype will return a number which will represent a date value in between January 1, 100 to December 31, 9999

#11) String: This subtype will return a variable-length string value which can approximately be up to 2 billion characters in length.

#12) Object: This subtype will return an object.

#13) Error: This subtype will return an error number.

How to Create a Simple VBScript?

To create a VBScript, there are only 2 things required.

They are:

  • Text Editors like Notepad++ or even Notepad to write the VBScript Code.
  • IE (good to have IE6 or above) to run the VBScript Code.

Now, let’s see a few VBScript Code for clarity purpose but before that, it is important to know where can the Scripts be inserted in an HTML Page.

Where to Insert Scripts in an HTML Page?

VBScript provides you the liberty to place a code in any of the following sections:

  • Within the Header Tags i.e. in between <head> and </head>.
  • Within the Document’s Body i.e. between <body> and </body> tags.

First VBScript Code in HTML:

Now, let’s take a simple example to understand how VBScript code can be written inside HTML tags.

<title> Testing VBScript Skills </title>
<script type=”text/vbscript”>
variable1 = 1
variable2 = 2
output = (variable1 + variable2) / 1
document.write (“resultant from the above equation is ” & output)

Note: Whatever is put inside the brackets of ‘document.write’, will be displayed as an output on the display page.

The Output of this program is: resultant from the above equation is 3

After completing the code, you can save this in a file and give a file name as anyfilename.html.

To run, just open this file in IE.

Important to Know:

We have just seen the implementation of VBScript code in the HTML file. However, VBScript in QTP is not placed inside the HTML tags. It is saved with an extension ‘.vbs’ and is executed by the QTP Execution Engine.

To understand the practical implementation of VBScript in terms of QTP, you must know variables, constants, etc. and I will cover that in my upcoming tutorials and for time being, I just want to show you the VBScript code with the concept of an external file.

VBScript in External File:

<script type=”text/vbscript” src=”nameofthefile.vbs”> </script>
variable1 = 22
variable2 = 21
subtraction = variable1 - variable2
document.write (“subtraction of 2 numbers is” & subtraction)

To access this code from an external source, save this code in a text file with an extension “.vbs”.

How Comments are Handled in VBScript

It is considered to be a good programming practice to include comments in the Scripts for better readability and understanding purposes.

There are 2 ways in which Comments can be handled in a VBScript:

#1) Any Statement that starts with a Single Quote (‘) is treated as a comment:

<script type=”text/vbscript”</script>
‘let’s do subtraction of 2 numbers 
variable1 = 11
variable2 = 10
subtraction = variable1 - variable2
document.write (“subtraction of 2 numbers is” & subtraction)

Single Quote

#2) Any Statements that start with the keyword REM are treated as Comments.

<script type=”text/vbscript”></script>
REM let’s do subtraction of 2 numbers           
variable1 = 11
variable2 = 10
subtraction = variable1 - variable2
document.write (“subtraction of 2 numbers is” & subtraction)


Formatting Tips:

#1) No Semicolon is required to end the particular statement in VBScript.

#2) If 2 or more lines are written in the same line in VBScript then Colons (:) act as a line separator.

Let’s understand this with the help of an Example:

<script type=”text/vbscript”></script>
variable1 = 11:variable2 = 21:variable3=34

Formatting Tips
If a statement is lengthy and required to break into multiple statements then you can use underscore “_”.

Let’s see its Example:

<script type=”text/vbscript”></script>
variable1 = 11
variable2 = 10
output = (variable1 - variable2) * 10
document.write (“output generated from the calculation”& _
“of using variable1 and variable2 with the multiplication of resultant”&_
from 10 is” & output)


Reserved Keywords

In any language, there are a set of words which work as Reserved Words and they cannot be used as Variable names, Constant names or any other Identifier names.

Following is the list of Reserved Keywords in VBScript:




That’s it! This is all about the basic concepts that are involved in VBScript.

I hope that this VBScript tutorial would have a given you a brief overview and clear understanding of this scripting language with easy examples.

About the author: Thanks to STH Team member Varsha for helping us to compile this series. She is in the Software Testing field with core VBScripting skills and received many internal excellence awards.

NEXT Tutorial #2 => In my upcoming VBS tutorial, I will cover about Variables in VBScript.

=> Visit Here For The QTP Training Tutorials Series

Stay tuned for much more updates, and feel free to share your thoughts about this tutorial.

Related Post

Leave a Reply

Your email address will not be published.