VbaFunctionsBeginner

Sub procedure

Defines a macro or procedure that performs actions without returning a value.

Review the syntaxStudy the examplesOpen the coding app
Sub MacroName()
    ' statements
End Sub

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Defines a macro or procedure that performs actions without returning a value.

A Sub procedure is the standard entry point for Excel automation. Use it for tasks that change workbooks, write values, format ranges, or orchestrate helper routines. It is the right choice for button-click macros and report refresh jobs, while Function is better when you need a return value.

Quick reference

Syntax

Sub MacroName()
    ' statements
End Sub

See it in practice

Examples

1

Simple workbook automation

Sub FillReportHeader()
    Range("A1").Value = "Weekly Sales Report"
    Range("A1").Font.Bold = True
End Sub

A Sub is ideal when the goal is to change workbook state rather than return a result.

Debug faster

Common Errors

1

Compile error

Cause: Forgetting End Sub or nesting one Sub inside another.

Fix: Keep each procedure self-contained and make sure every Sub ends with End Sub.

Runtime support

Compatibility

Excel desktop VBA

VBA macros do not run in Excel for the web or Google Sheets.

Source: Microsoft Learn Office VBA reference

Common questions

Frequently Asked Questions

Defines a macro or procedure that performs actions without returning a value.

Compile error: Keep each procedure self-contained and make sure every Sub ends with End Sub.