VBA Projects

Full Access with Source Code
  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

Manage Your Projects

120+ Project Management Templates

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Share Post

The Sub statement is a fundamental aspect of the VBA programming language. It stands for “subroutine” and is used to define a procedure or subroutine within a VBA module. Subroutines are simply a set of instructions or code that can be executed by calling its name within a program. The Sub statement is used to create a new subroutine, while the ‘End Sub’ statement is used to mark the end of the subroutine. In this blog post, we will discuss the purpose, syntax, examples, important notes and remarks, and conclude with feedback and views on the Sub statement in VBA.

VBA Sub Statement


The main purpose of the Sub statement is to create reusable code blocks that can be called multiple times within a program. This allows for more efficient and organized coding by separating different functions and procedures into smaller subroutines. Additionally, subroutines can be called from other parts of the program, making it easier to debug and troubleshoot any errors that may arise. Overall, the Sub statement helps to improve the structure, readability, and maintainability of VBA code.


The syntax for the Sub statement in VBA is as follows:

Sub name()
    ' Code block
End Sub

The name of the subroutine can be anything as long as it follows the naming rules for VBA variables (starts with a letter, no spaces, etc.). The parentheses after the name are optional and can be left empty or can include parameters that are passed to the subroutine. The code block between the Sub and ‘End Sub’ statements is where the instructions and statements for the subroutine are written.

Examples of VBA Sub Statement

Example 1: A Simple Subroutine

In this example, we create a simple subroutine ‘PrintMessage’ that prints a message when called.

Sub PrintMessage()
    MsgBox "Hello, world!"
End Sub

To execute this subroutine, we can simply call its name from another subroutine or from the immediate window in the VBA editor.

Sub Main()
End Sub

The ‘PrintMessage’ subroutine will display a message box with the text “Hello, world!” when executed.

Example 2: Subroutine with Parameters

In this example, we create a subroutine ‘AddNumbers’ that takes two parameters and returns their sum.

Sub AddNumbers(num1 As Integer, num2 As Integer)
    MsgBox "The sum of " & num1 & " and " & num2 & " is: " & num1 + num2
End Sub

To call this subroutine, we must pass two integer values as arguments for the parameters.

Sub Main()
    AddNumbers 5, 7
End Sub

The ‘AddNumbers’ subroutine will return a message box with the sum of the two numbers, in this case, 12.

Example 3: Subroutine with a Loop

In this example, we create a subroutine ‘PrintNumbers’ that uses a loop to print numbers from 1 to a specified limit.

Sub PrintNumbers(limit As Integer)
    Dim i As Integer
    For i = 1 To limit
        Debug.Print i
    Next i
End Sub

The ‘PrintNumbers’ subroutine can be called with a limit parameter, and it will use a ‘For’ loop to print numbers from 1 to the specified limit in the Immediate window.

Sub Main()
    PrintNumbers 10
End Sub

This will print the numbers 1 to 10 in the Immediate window.

Example 4: Subroutine for Cell Formatting

In this example, we create a subroutine ‘FormatCell’ that takes a cell reference as a parameter and applies a bold font and red color to its contents.

Sub FormatCell(cell As Range)
    cell.Font.Bold = True
    cell.Font.Color = RGB(255, 0, 0)
End Sub

To use this subroutine, we can pass a cell reference as an argument.

Sub Main()
    FormatCell Range("A1")
End Sub

This will format the contents of cell A1 with a bold font and red color.

Example 5: Subroutine for Finding and Replacing Text

In this example, we create a subroutine ‘FindAndReplace’ that takes two strings as parameters and replaces any occurrences of the first string with the second string in a given cell.

Sub FindAndReplace(findStr As String, replaceStr As String, cell As Range)
    cell.Value = Replace(cell.Value, findStr, replaceStr)
End Sub

To use this subroutine, we can pass the two strings to be replaced and the cell reference as arguments.

Sub Main()
    FindAndReplace "apples", "oranges", Range("A1")
End Sub

This will search for any instances of “apples” in cell A1 and replace them with “oranges”.

Important Notes & Remarks

  • The Sub statement can only be used within VBA modules, not in a worksheet cell or userform.
  • Subroutines can be called from other subroutines, functions, or even from worksheet cells using the Application.Run method.
  • If a Sub statement ends with a ‘Return’ statement, it will exit the subroutine without executing any code after the ‘Return’.
  • Subroutines cannot return values, unlike functions in VBA. They are used for carrying out tasks or operations.
  • Subroutines can have their own set of local variables that are only accessible within the subroutine.

In conclusion, the Sub statement in VBA is a crucial part of the language that helps to organize and improve the efficiency of coding. It allows for the creation of reusable subroutines that can be called from different parts of a program. In this blog post, we discussed the purpose, syntax, provided five examples, and highlighted some important notes and remarks for the Sub statement.

What do you think about the Sub statement in VBA? Have you come across any interesting or useful ways of using subroutines in your projects? Let us know in the comments below. Your feedback and views are valuable to us.

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBA StatementsTags: , Last Updated: September 28, 2023

Leave A Comment