What general tips do you have for golfing in VBScript? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to VBScript. Please post one tip per answer.

Thanks to Marcog for the original idea. :)

  • \$\begingroup\$ My friends would like this :) \$\endgroup\$
    – ckjbgames
    Mar 29, 2017 at 1:41
  • \$\begingroup\$ Just in case anyone is editing vbs in notepad and just running the file - you can save yourself from having to see lots of MsgBoxs by replacing the MsgBoxs with WScript.Echos and running the VBScipt file in cmd.exe with CScript (CScript C:\filepath\file.vbs) \$\endgroup\$ Jan 25, 2018 at 4:30
  • \$\begingroup\$ Oh and you can add syntax highlighting for VBScript (or any VB derivative) using the markdown notation <!-- language-all: lang-vb --> \$\endgroup\$ Jan 25, 2018 at 4:32

5 Answers 5


Use an empty variable instead of ""

Uninitialized variables are set to a special value: empty. When empty is cast to a string it becomes "".

Contrived example (using z instead of ""):

For i=1To 5
    For j=1To i
    MsgBox r

For numeric types Empty is 0, and for booleans it is false. This can cut down on initialization code.

a=inputbox("Enter Starting Value")
while done=0
    done=(b>10000 or i>10)
msgbox "Your "&a&" rabbit(s) have turned into "&b&" rabbit(s) in "&i&" years."

Use IIf instead of If...Then

IIf() is essentially a ternary operator. You can potentially save quite a few bytes.

For example:

var = "World"

If foo Then
    MsgBox "Hello" & var
    MsgBox "Goodbye" & var
End If 


MsgBox IIf(foo, "Hello", "Goodbye") & "World"

Note: VBA only. VBScript doesn't support this one.

  • \$\begingroup\$ Iff() isn't an intrinsic VBScript function, thus it's not supported. \$\endgroup\$ Dec 5, 2015 at 11:15
  • \$\begingroup\$ Well, I'll be damned. I can't believe I never knew knew that. You're right. I'm going to leave it here because the tip will be useful for vba folks. \$\endgroup\$
    – RubberDuck
    Dec 5, 2015 at 11:35
  • 2
    \$\begingroup\$ Unfortunately Iff() isn't an intrinsic VBA function as well :D \$\endgroup\$ Dec 5, 2015 at 11:42
  • \$\begingroup\$ Yes it is. Oh. Lol. Damned typo. \$\endgroup\$
    – RubberDuck
    Dec 5, 2015 at 11:43

Shorter IF Then: save 10 bytes

If you terminate an IF/THEN clause with a carriage return, you don't need to use an END IF.


For x=1 to 1
  IF x=1 THEN MSGBOX "No Error"

Use Line Separators

Newlines count as two bytes ("\n"), but a colon is only one.

For i = 0 To 10
    MsgBox "Hello"


For i = 0 To 10: MsgBox "Hello": Next

Note: You can't use the line separator for If statements.

  • \$\begingroup\$ Newlines only count as one byte on Unix Systems, however, Windows Systems use \r\n (Which is two bytes, 13 and 10 respectively) \$\endgroup\$
    – ATaco
    Mar 29, 2017 at 1:20

Condense For statements

In For statements (and in fact any statement comprised of a " or numeric literal followed by a reserved command) the space that separates any numeric and a following word may be removed

This means that

For i = 1 To 100 Step 2

May be condensed down to

For i=1To 100Step 2

and following this logic,

If t = "TEST" Then ...

may be condensed to

If t="Test"Then ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.