Sub procedure
Defines a macro or procedure that performs actions without returning a value.
Sub MacroName()
' statements
End SubThis 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
Simple workbook automation
Sub FillReportHeader()
Range("A1").Value = "Weekly Sales Report"
Range("A1").Font.Bold = True
End SubA Sub is ideal when the goal is to change workbook state rather than return a result.
Debug faster
Common Errors
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
VBA macros do not run in Excel for the web or Google Sheets.
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.