AI Generate VBScript docs instantly

VBScript Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

4 snippets

Basic VBScript syntax and first scripts

Hello World

WScript.Echo "Hello, World!"

Message Box

MsgBox "Hello, World!"
MsgBox "Alert", vbExclamation, "Warning"

Comments

' Single line comment
REM Another comment style

Run Script

' Save as script.vbs
cscript script.vbs    ' Console
wscript script.vbs    ' GUI

Variables & Data Types

5 snippets

Variable declaration and types

Variable Declaration

Dim name
name = "John"

Dim age, city
age = 30
city = "NYC"

Constants

Const PI = 3.14159
Const MAX_SIZE = 100

Data Types

Dim num    ' Integer or Long
Dim price  ' Double
Dim text   ' String
Dim flag   ' Boolean
Dim today  ' Date

Type Conversion

CInt("42")        ' To Integer
CDbl("3.14")      ' To Double
CStr(100)         ' To String
CBool(1)          ' To Boolean
CDate("1/1/2024") ' To Date

Check Variable Type

If IsNumeric(value) Then
  WScript.Echo "Number"
End If

If IsDate(value) Then
  WScript.Echo "Date"
End If

Operators

4 snippets

Arithmetic and comparison operators

Arithmetic

result = 10 + 5      ' Addition
result = 10 - 5      ' Subtraction
result = 10 * 5      ' Multiplication
result = 10 / 5      ' Division
result = 10 Mod 3    ' Modulus
result = 2 ^ 3       ' Exponentiation

Comparison

If a = b Then        ' Equal
If a <> b Then       ' Not equal
If a > b Then        ' Greater than
If a < b Then        ' Less than
If a >= b Then       ' Greater or equal
If a <= b Then       ' Less or equal

Logical Operators

If a And b Then      ' AND
If a Or b Then       ' OR
If Not a Then        ' NOT
If a Xor b Then      ' XOR

String Concatenation

fullname = firstname & " " & lastname
message = "Hello, " & name & "!"

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Control Flow

5 snippets

Conditional statements and branching

If Statement

If age >= 18 Then
  WScript.Echo "Adult"
End If

If-Else

If score >= 60 Then
  WScript.Echo "Pass"
Else
  WScript.Echo "Fail"
End If

If-ElseIf-Else

If grade >= 90 Then
  result = "A"
ElseIf grade >= 80 Then
  result = "B"
ElseIf grade >= 70 Then
  result = "C"
Else
  result = "F"
End If

Select Case

Select Case day
  Case "Monday"
    WScript.Echo "Start of week"
  Case "Friday"
    WScript.Echo "End of week"
  Case Else
    WScript.Echo "Midweek"
End Select

Inline If (IIf)

result = IIf(age >= 18, "Adult", "Minor")
status = IIf(score >= 60, "Pass", "Fail")

Loops

7 snippets

Iteration constructs

For Loop

For i = 1 To 10
  WScript.Echo i
Next

For Loop with Step

For i = 0 To 100 Step 10
  WScript.Echo i
Next

For i = 10 To 1 Step -1
  WScript.Echo i  ' Countdown
Next

For Each Loop

Dim fruits
fruits = Array("apple", "banana", "cherry")

For Each fruit In fruits
  WScript.Echo fruit
Next

While Loop

Dim count
count = 0

While count < 5
  WScript.Echo count
  count = count + 1
Wend

Do While Loop

Dim i
i = 0

Do While i < 5
  WScript.Echo i
  i = i + 1
Loop

Do Until Loop

Dim i
i = 0

Do Until i >= 5
  WScript.Echo i
  i = i + 1
Loop

Exit Loop

For i = 1 To 100
  If i = 50 Then
    Exit For
  End If
  WScript.Echo i
Next

Functions & Procedures

4 snippets

Reusable code blocks

Sub Procedure

Sub Greet(name)
  WScript.Echo "Hello, " & name
End Sub

Greet "World"

Function with Return

Function Add(a, b)
  Add = a + b
End Function

result = Add(5, 3)
WScript.Echo result

Function with Multiple Returns

Function Calculate(x, y)
  If y = 0 Then
    Calculate = -1
  Else
    Calculate = x / y
  End If
End Function

ByRef vs ByVal

Sub ModifyValue(ByRef x, ByVal y)
  x = x * 2   ' Modifies original
  y = y * 2   ' Doesn't affect original
End Sub

Arrays

5 snippets

Working with arrays

Array Declaration

Dim fruits(2)
fruits(0) = "apple"
fruits(1) = "banana"
fruits(2) = "cherry"

Dynamic Array

Dim numbers()
ReDim numbers(5)
numbers(0) = 10

ReDim Preserve numbers(10)  ' Keep data

Array Function

Dim fruits
fruits = Array("apple", "banana", "cherry")

WScript.Echo fruits(0)  ' apple

Multi-Dimensional Array

Dim matrix(2, 2)
matrix(0, 0) = 1
matrix(0, 1) = 2
matrix(1, 0) = 3

Array Bounds

lower = LBound(fruits)  ' 0
upper = UBound(fruits)  ' 2
size = UBound(fruits) - LBound(fruits) + 1

File System Operations

6 snippets

Reading and writing files

Create FileSystemObject

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Write to File

Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("output.txt", True)
file.WriteLine "Hello, World!"
file.Close

Read from File

Dim fso, file, content
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("input.txt", 1)
content = file.ReadAll
file.Close
WScript.Echo content

Append to File

Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("log.txt", 8, True)
file.WriteLine "Log entry"
file.Close

Check File Exists

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists("test.txt") Then
  WScript.Echo "File exists"
End If

Copy/Delete Files

fso.CopyFile "source.txt", "dest.txt"
fso.DeleteFile "temp.txt"
fso.MoveFile "old.txt", "new.txt"

More Cheat Sheets

FAQ

Frequently asked questions

What is a VBScript cheat sheet?

A VBScript cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in VBScript. It helps developers quickly look up syntax without searching through documentation.

How do I learn VBScript quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important VBScript concepts?

Key VBScript concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my VBScript code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your VBScript code using AI.

Code Conversion Tools

Convert VBScript to Other Languages

Easily translate your VBScript code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate VBScript Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week