Displays a modal message box. The buttons argument is a sum of values specifying the buttons, icon, and default button.
| Constant | Value | Description |
|---|---|---|
| vbOKOnly | 0 | Display OK button only. |
| vbOKCancel | 1 | Display OK and Cancel buttons. |
| vbAbortRetryIgnore | 2 | Display Abort, Retry, and Ignore buttons. |
| vbYesNoCancel | 3 | Display Yes, No, and Cancel buttons. |
| vbYesNo | 4 | Display Yes and No buttons. |
| vbRetryCancel | 5 | Display Retry and Cancel buttons. |
| Constant | Value | Icon Style |
|---|---|---|
| vbCritical | 16 | Critical Message icon. |
| vbQuestion | 32 | Warning Query icon. |
| vbExclamation | 48 | Warning Message icon. |
| vbInformation | 64 | Information Message icon. |
| Value | Constant | Button Clicked |
|---|---|---|
| 1 | vbOK | OK |
| 2 | vbCancel | Cancel |
| 3 | vbAbort | Abort |
| 4 | vbRetry | Retry |
| 5 | vbIgnore | Ignore |
| 6 | vbYes | Yes |
| 7 | vbNo | No |
Dim res
res = MsgBox("Save changes?", vbYesNoCancel + vbQuestion, "Confirm")
If res = vbYes Then
' Save logic
End IfPrompts user for text input. Returns the string entered or empty string if Cancelled.
name = InputBox("User name:", "Login", "Guest")Launches programs. The second parameter controls window style (0=Hidden, 1=Normal, 2=Minimized).
Set shell = CreateObject("WScript.Shell")
' Launch Notepad hidden
shell.Run "notepad.exe", 0Utilizes Windows shutdown tool. -s (shutdown), -r (restart), -l (logoff).
' Shutdown PC in 60 seconds with comment
shell.Run "shutdown -s -t 60 -c ""VBS Automated Shutdown"""Extracts a substring from start position. Note: VBS is 1-indexed.
Mid("Hello", 2, 2) ' Returns "el"Returns the numeric position of a search term within a string. Returns 0 if not found.
pos = InStr(1, "find the needle", "needle") ' returns 10Swaps all occurrences of a substring for another.
Replace("Old Text", "Old", "New") ' "New Text"Converts string to Array and back. Essential for CSV processing.
colors = Split("red;blue;green", ";")
list = Join(colors, ", ")Returns a random number between 0 and 1. Always call Randomize first to seed the generator.
Randomize
dice = Int((6 * Rnd) + 1) ' 1 to 6Absolute value and Square Root respectively.
dist = Abs(-50) ' 50
root = Sqr(16) ' 4Calculates time between two dates.
| Interval | Meaning |
|---|---|
| "yyyy" | Year |
| "m" | Month |
| "d" | Day |
| "h" | Hour |
daysLeft = DateDiff("d", Now, "12/25/2026")Cleaner alternative to multiple If statements when checking a single variable.
Select Case grade
Case "A": MsgBox "Great!"
Case "B": MsgBox "Good"
Case Else: MsgBox "Try harder"
End SelectIterates through collections (Files, Network drives, Registry keys).
For Each file In folder.Files
WScript.Echo file.Name
NextReturns Boolean. Essential for validating user input before performing math.
If IsNumeric(input) Then result = input * 2Returns a code representing the technical data type.
| Code | Constant | Type |
|---|---|---|
| 2 | vbInteger | Integer |
| 8 | vbString | String |
| 11 | vbBoolean | Boolean |