Sub - VBScript


The Sub command in VB Script marks the beginning of a subroutine, a reusable block of code within a script. Subroutines allow for code modularity and can enhance script organization and readability.


Sub subroutine_name(parameters)
    ' Code to be executed
    ' ...
End Sub


  • subroutine_name: Name of the subroutine.
  • parameters: Optional arguments passed to the subroutine.




Simple Subroutine:

Sub GreetUser
    MsgBox "Hello, world!"
End Sub

Subroutine with Parameters:

Sub CalculateSum(num1, num2)
    MsgBox num1 + num2
End Sub

Common Issues

  • Undefined subroutine name: Ensure the subroutine name is spelled correctly and defined before calling it.
  • Incorrect number of parameters: Check if the number of parameters passed to the subroutine matches the number defined in the declaration.


Subroutines can be called from other subroutines or events within the script. For example:

Sub EventProcedure
    Call GreetUser
End Sub
  • Function: Defines a function that returns a value.
  • Procedure: Similar to Sub but does not require a name.