Skip to the stories
Excel & Power BI

VBA Sub Procedures: Write, Run and Organize Excel Macros

VBA Sub procedures perform actions in Excel, from formatting reports to moving data. Learn where to write them, how to run them and when to use a function.

VBA Sub Procedures: Write, Run and Organize Excel Macros

A VBA Sub procedure is the code that carries out an Excel macro’s work. It can change workbook data, format a report or save a copy of a file. For administrators and Excel power users, the practical distinction is simple: a Sub performs actions, while a function calculates and returns a value that can be used in a formula.

What a Sub procedure does

When Excel records a macro, it creates a procedure. Running that macro from a button also runs a procedure. The code may sort a list, copy data between sheets, create a workbook, display a message, find inconsistent values, remove empty rows or save a workbook copy to a folder. These are actions applied to the workbook or its data.

In VBA, a procedure is called a Sub, short for subroutine. It begins with Sub, followed by a name and parentheses, and ends with End Sub. The instructions between those lines execute in order, from top to bottom.

Sub MyFirstProgram()
    Dim MyNumber As Long

    For MyNumber = 1 To 10
        Cells(MyNumber, 1) = MyNumber
        Cells(MyNumber, 2) = MyNumber + MyNumber
        Cells(MyNumber, 3) = MyNumber * MyNumber
    Next MyNumber
End Sub

This example loops through the numbers 1 to 10. For each number, it writes the number, its double and its square into columns A, B and C, respectively. The variable MyNumber is declared as a Long, and the loop advances until it reaches 10.

image excel exercises

Choose a name that describes the task, such as FormatReport or CopyToArchive. Procedure names can’t contain spaces or begin with a number. The parentheses are part of the procedure declaration, even when no arguments appear between them. Clear names help anyone maintaining the workbook understand what each macro is meant to do before reading its instructions.

Where to put the code

Open the VBA editor with Alt + F11. If the Developer tab isn’t visible in Excel, use the instructions for enabling the Developer tab. In the editor, choose Insert and then Module. Type the procedure into the module’s code window.

A standard module gives you a place to store procedures that can be run as macros. For a first exercise in the editor, see First Steps with VBA: Discover the VBA Editor. The example shown here writes its results to worksheet cells, so you can compare the code’s instructions with the values it produces.

image excel quizzes
VBA procedure MyFirstProgram in Module_Init and its result in the worksheet

Ways to run a procedure

You can start a procedure from either the VBA editor or Excel. In the editor, click anywhere inside the procedure and press F5. From Excel, press Alt + F8, choose the macro from the list and click Run. For a workbook users will run repeatedly, add a button from the Developer tab and assign the procedure to it.

These options serve different work patterns. F5 is convenient while developing or checking a macro in the editor. The macro list provides a direct way to run an available procedure from Excel. A button puts the macro closer to the task it supports, such as preparing a report. Whichever entry point you use, it starts the same procedure and its instructions run in sequence.

Break larger macros into steps

A long procedure can be easier to follow when its work is divided into smaller procedures. A main Sub can call each step in order, with names that describe the task being handed off:

Sub MonthlyReport()
    ImportData
    FormatReport
    PrintReport
End Sub

Here, MonthlyReport runs ImportData, then FormatReport, and then PrintReport. Each called procedure holds the instructions for its part of the workflow. This structure makes the main routine read like a short sequence of tasks and lets you work on one step without crowding the others into the same block of code.

You can make a call explicit by writing Call ImportData. In the example, the effect is the same as writing ImportData by itself; Call simply makes the handoff more visible. This is useful when reading a procedure with several steps, especially when you want to spot where one routine starts another.

Choose a function when Excel needs a value

A Sub can calculate a result and write it into a worksheet cell, but it doesn’t return a value for Excel to use as a formula result. If you want a calculation—such as a column total with 20% tax—to be entered in a cell like a worksheet formula, use a VBA function. Functions calculate a value and return it, in the way familiar Excel functions such as SUM or VLOOKUP do.

That distinction helps determine the shape of the code before you start: use a Sub when the goal is to perform actions, and a function when the caller needs a returned value. For a next step, see What Is a Function in VBA?. You can also practise related Excel skills with online Excel exercises.

Source: Frédéric LE GUEN · excel-tutorial.com

Same flag