Software >> OS >> Windows >> Scripting >> VBScript >> Cheatsheet

 VBScript Versions

https://msdn.microsoft.com/en-us/library/4y5y7bh5.aspx

Topic Feature Syntax/Description/Examples
 Comments Put comments in your code  Prefix with ' (aphostrophe) or Rem
 Array    
 
Function to create an Array
A = Array(10,20,30)
Declare an Array variable  

Eg. 1

Dim NameArray(9)  
'Declares static array with 10 elements, 
'LoweBound is 0
'Upperbound is 9
 

Dim Contacts(9,9)
'Declares 2 dimension array with 10x10 element


Eg. 3

Dim Names()
'Declare dynamic array

Test for an Array  
Dim MyArray(3)
MyArray(0) = "Sunday"
MyArray(1) = "Monday"
MyArray(2) = "Tuesday"
MyVariable = IsArray(MyArray) ' MyVariable contains "True"
Get Lowest subscript for the Array  

E.g. for single dimension Array

LBound(MyArray)
' returns the lowest subscript of the 1st/only dimension of the array

E.g. for multi-dimention Array

LBound(MyArray,2)
' returns the lowest subscript for the 2nd dimension of the array

Get the highest subscript for the Array  UBound(MyArray) returns the subscript of the last element of the array
 Functions  
  
Conversion Functions  
CBool Converts an expression to a Boolean subtype.
CByte Converts an expression to a Byte subtype.
CCur Converts an expression to a Currency subtype.
CDate Converts an expression to a Date subtype.
CDbl Converts an expression to a Double subtype.
CInt Converts an expression to a Integer subtype.
CLng Converts an expression to a Long subtype.
CSng Converts an expression to a Single subtype.
CStr Converts an expression to a String subtype.
Format Functions  
ormatCurrency Returns an expression formatted as a currency value.
FormatDateTime Returns an expression formatted as a date or time.
FormatNumber Returns an expression formatted as a number.
FormatPercent Returns an expression formatted as a percent value with a trailing percent (%) symbol.
String Functions  
Join Returns a string created by joining strings stored in an array.
Filter Returns a zero-based array containing subset of a string array based on a filter criteria.
InStr Returns the numeric position of the first occurrence of one string within another.
InStrRev Returns the numeric position of the first occurrence of one string within another starting from the end of the string.
LCase Returns a string with all its uppercase letters converted to lowercase letters.
Left Returns a specified number of characters from the left side of a string.
Len Returns the number of characters in a string.
LTrim Removes all the spaces from the left side of a string.
RTrim Removes all the spaces from the right side of a string.
Trim Removes spaces from both the left and the right sides of a string.
Mid Returns a specified number of characters from a string.
Replace Returns a string in which a specified sub-string has been replaced with another substring a specified number of times.
Right Returns a specified number of characters from the right side of a string.
Space Returns a string consisting of the number of defined spaces.
Split Returns a zero-based, one-dimensional array that contains a specified number of substrings.
StrComp Compares two strings and returns a value indicating the result of the comparison.
String Returns a string consisting of character repeated number times.
StrReverse Returns a string where the character order has been reversed.
UCase Returns string with all its lowercase letters converted to uppercase letters.
Math Functions  
Abs Returns the absolute value of a number.
Atn Returns the arctangent of a number.
Cos Returns the cosine of an angle.
Exp Returns e, the base of natural logarithms, raised to a power.
Hex Returns a string representing the hexadecimal value of a number.
Int Returns the integer portion of a number. If number is negative, Int returns the first integer less than or equal to number.
Fix Returns the integer portion of a number. If number is negative, Fix returns the first integer greater than or equal to number.
Log Returns the natural logarithm of a number.
Oct Returns a string representing the octal value of a number.
Rnd Returns a random number less than 1 but greater or equal to 0.
Round Returns a number rounded to a specified number of decimal places.
Sgn Returns an integer indicating the sign of a number.
Sin Returns the sine of an angle.
Sqr Returns the square root of a number.
Date Functions  
Date Returns the current system date.
DateAdd Returns a date to which a specified time interval has been added.
DateDiff Returns the number of intervals between two dates.
DatePart Returns the specified part of a given date.
DateSerial Returns a date for a specified year, month, and day.
DateValue Returns a date.
Day Returns a number representing the day of the month (between 1 and 31, inclusive).
Hour Returns a number representing the hour of the day (between 0 and 23, inclusive).
Minute Returns a number of the minutes in current system time (between 0 and 59, inclusive).
Month Returns a number representing  the month of the year (between 1 and 12, inclusive).
MonthName Returns the name of a specified month.
Now Returns the current system date and time.
Second Returns the current seconds value of the current system time (between 0 and 59, inclusive).
Time Returns the current system time.
TimeSerial Returns the time for a specific hour, minute, and second.
TimeValue Returns the current system time.
Weekday Returns a number representing the day of the week (between 1 and 7, inclusive).
WeekdayName Returns the weekday name of a specified day of the week.
Year Returns a number representing the year.
Type checking Functions  
VarType Returns an integer code that indicates the subtype of a variable.
TypeName Returns the subtype of a variable.
IsNumeric Returns a Boolean value indicating whether an expression can be evaluated as a number.
IsArray Returns a Boolean value indicating whether a variable is an array.
IsDate Returns a Boolean value indicating whether the expression can be converted to a date.
IsEmpty Returns a Boolean value indicating whether a variable has been initialized or not.
IsNull Returns a Boolean value that indicates whether an expression contains no valid datatype.
IsObject Returns a Boolean value indicating whether an expression refers to an Automation object.
Other Functions  
Chr Returns the character specified by the ANSI character code.
Asc Returns the ANSI character code of a string.
 Variables Assigning values to variables

Numeric

A = 200
B = 99.9

Date or Time

CutoffDate = #06/18/2008#
CutoffTime = #3:36:00 PM#

String

Name = "VBScript"
 Contants Declaring Constants Const MyString = "This is my string"
Const MyAge = 49  
Const CutoffDate = #06/18/2008#  
Const CutoffTime = #3:36:00 PM#
 
 Operators 
 
Arithmetic Operators  

Description

Symbol

Exponentiation

^

Unary negation

-

Multiplication

*

Division

/

Integer division

\

Modulus arithmetic

Mod

Addition

+

Subtraction

-

String concatenation

&

 Comparison Operators  

Description

Symbol

Equality

=

Inequality

<>

Less than

<

Greater than

>

Less than or equal to

<=

Greater than or equal to

>=

Object equivalence

Is

 Logical Operators  

Description

Symbol

Logical negation

Not

Logical conjunction

And

Logical disjunction

Or

Logical exclusion

Xor

Logical equivalence

Eqv

Logical implication

Imp

 Conditional Statements
 
If .. Then .. Else

 

' Block syntax: 
If condition Then
   [statements]
[ElseIf condition-n Then
   [elseifstatements]] . . .
[Else
   [elsestatements]]
End If 

Single-Line syntax:
If condition Then statements [Else elsestatements ]

Multi-line example

dim count
count = 0

If count = 0 Then
    MsgBox "There are no items."
ElseIf count = 1 Then
    MsgBox "There is 1 item."
Else
    MsgBox "There are " & count & " items."
End If

Single line example 
If A > 10 Then A = A + 1 : B = B + A : C = C + B
 Select .. Case  
Select Case testexpression
   [Case expressionlist-n
      [statements-n]] . . .
   [Case Else
      [elsestatements-n]]
End Select

Example

Dim Color, MyVar
Sub ChangeBackground (Color)
   MyVar = lcase (Color)
   Select Case MyVar
      Case "red"     document.bgColor = "red"
      Case "green"   document.bgColor = "green"
      Case "blue"    document.bgColor = "blue"
      Case Else      MsgBox "pick another color"
   End Select
End Sub
 
 Loop Do .. Loop

Example 1

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

Example 2

Sub ChkLastWhile()
   Dim counter, myNum
   counter = 0
   myNum = 9
   Do
      myNum = myNum - 1
      counter = counter + 1
   Loop While myNum > 10
   MsgBox "The loop made " & counter & " repetitions."
End Sub

Example 3

Sub ChkFirstUntil()
   Dim counter, myNum
   counter = 0
   myNum = 20
   Do Until myNum = 10
      myNum = myNum - 1
      counter = counter + 1
   Loop
   MsgBox "The loop made " & counter & " repetitions."
End Sub

Example 4

Sub ChkLastUntil()
   Dim counter, myNum
   counter = 0
   myNum = 1
   Do
      myNum = myNum + 1
      counter = counter + 1
   Loop Until myNum = 10
   MsgBox "The loop made " & counter & " repetitions."
End Sub
While .. Wend  
For .. Next  

Example 1

Sub DoMyProc50Times()
   Dim x
   For x = 1 To 50
      MyProc
   Next
End Sub

Example 2

Sub TwosTotal()
   Dim j, total
   For j = 2 To 10 Step 2
      total = total + j
   Next
   MsgBox "The total is " & total
End Sub

Example 3

Sub NewTotal()

   Dim myNum, total
   For myNum = 16 To 2 Step -2
      total = total + myNum
   Next
   MsgBox "The total is " & total
End Sub
For each .. Next  
Sub cmdChange_OnClick
   Dim d   'Create a variable 
   Set d = CreateObject("Scripting.Dictionary")
   d.Add "0", "Athens"   'Add some keys and items
   d.Add "1", "Belgrade"
   d.Add "2", "Cairo"

   For Each I in d
      Document.frmForm.Elements(I).Value = D.Item(I)
   Next
End Sub
     

 References

[1] https://msdn.microsoft.com/en-us/library/d1wf56tt.aspx

[2] https://msdn.microsoft.com/en-us/library/0ad0dkea.aspx