A Sub in Excel VBA is a subroutine: a named block of Visual Basic for Applications code that performs one or more actions and then ends. It is the standard, formal programming term VBA uses, and it is written between a Sub line and an End Sub line. A Sub is like a Mini Program that does work but does not return a value to whoever called it. If you have ever recorded a macro in Excel and opened the result in the Visual Basic Editor, you have already seen one: the recorder always writes your steps into a Sub, usually called something like Sub Macro1().
The short answer for practical purposes: "sub" in VBA means subroutine, the container you put macro code in. It is not slang and not an abbreviation of "substitute" or "subscription." It is the actual keyword of the language, and it is one of only two procedure types in VBA, the other being Function.
Where the Word Comes From
"Sub" is shorthand for subroutine, a term that predates personal computers by decades. Early programming languages had the idea of a "subprogram" or "subroutine": a section of code kept apart from the main program so it could be called whenever needed and reused instead of retyped. BASIC dialects, which VBA descends from in spirit, used the same vocabulary, and when Microsoft built Visual Basic and then VBA as the macro language inside Office applications, the keyword was simply Sub.
So in VBA the word has a precise technical meaning. It is standard terminology, not informal shorthand, and Microsoft's own documentation uses it. The casual habit of saying "a sub" instead of "a subroutine" is just everyday programmer speech.
What a Sub Looks Like
The structure is always the same. Open the Visual Basic Editor with Alt+F11, insert a module through the Insert menu, and type:
Sub SayHello() MsgBox "Hello from Excel"End Sub
Press F5 with the cursor inside the block and Excel runs it. The pieces break down like this:
SubandEnd Submark the beginning and end of the block. Everything between them is the body.SayHellois the procedure name. You choose it; name it something descriptive likeFormatReportrather thanSub1.- The parentheses hold an optional parameter list. Empty parentheses mean the Sub takes no arguments.
- The body is any legal VBA statement: writing to cells, opening workbooks, displaying dialog boxes, looping through ranges.
A Sub can also accept parameters and be declared Public or Private. Public procedures are visible to other modules and shown in the macro list; Private ones are hidden from the macro dialog and only callable from code in the same module. That is a useful way to keep helper routines out of the list a user sees when they press Alt+F8.
Why VBA Has Two Procedure Types
The distinction between Sub and Function is the single most useful thing to understand about subroutines.
- A Sub performs an action. It runs, does something, and returns nothing. You call it with a plain statement:
SayHelloorCall SayHello. - A Function performs a calculation and hands a value back. You use it inside an expression:
total = AddTax(price), or directly in a worksheet cell as a user-defined function.
If you find yourself wanting a subroutine to "give back" a result, the clean answer is usually to write a Function instead. The alternative is a Sub that writes its answer into a cell or into a module-level variable, which works but makes the code harder to follow and to reuse. A related misconception is that a Sub can be entered into a cell like =MySub(). It cannot. Excel's function list only exposes Functions; Subs appear only in the macro list.
How Subs Are Actually Used in Excel
Most Excel users meet Subs in one of a few patterns.
- Macro recording. The recorder produces a Sub. You then edit it, perhaps replacing fixed ranges with variables so it works on any selection.
- Event handlers. Subs named after events, such as
Worksheet_ChangeorWorkbook_Open, run automatically when the event fires. These live in the sheet or workbook object modules, not in ordinary modules, and their names must match exactly. - Button and shape triggers. Right-click a shape or button, choose Assign Macro, and pick a Sub. Clicking runs it.
- Orchestration. A short master Sub calls several smaller Subs in order, each doing one clearly named step. This keeps long procedures readable and testable.
A worked example: a Sub that stamps the current time into the active cell.
Sub StampTime() ActiveCell.Value = Now ActiveCell.NumberFormat = "mmmm d, yyyy h:mm AM/PM"End Sub
Assign it to a button, and you have a one-click timestamp. Note the limits: it acts on whatever cell is active, so the user must select the right cell first. A more robust version would check that a single cell is selected before writing.
Common Problems and How to Fix Them
Most Sub failures fall into a small set of well-known traps.
- Compile error: Expected End Sub. A block is missing its closing line, or there is a stray
End Subinside another Sub. Each Sub must be fully closed before the next begins. - Macro not in the list. It may be declared Private, it may sit in a module that is not a standard module (object modules hide their Subs), or macros may be disabled by the Trust Center settings.
- Ambiguous name detected. Two procedures in the same scope share a name. Rename one.
- Nothing happens, or the wrong sheet changes. Code that relies on
ActiveSheetorActiveCellfollows the user's focus. Name the specific worksheet and range in the code wherever you can. - Endless loop inside a Sub. A loop with no exit condition locks Excel. Open the VBA Editor and press the Reset button on the toolbar to stop execution.
- Unsaved work. A misbehaving macro can modify or delete data. Save the workbook first, or work on a copy, and consider switching off ScreenUpdating while the code runs for speed.
If a recorded macro fails after a layout change, read the generated lines closely. The recorder is literal: it writes the exact address, sheet name, and number of rows it saw at the time. Editing those hard-coded references into variables is usually the fix.
Subs Versus Related VBA Terms
Readers often confuse adjacent terms, so a quick comparison helps.
- Macro: the everyday name for anything you can trigger from Excel's interface. Every no-argument Public Sub in a standard module is a macro; not every Sub is one.
- Procedure: the umbrella term covering both Subs and Functions.
- Module: the file-like container that holds Subs and Functions. A single module can hold many procedures.
- Sub in other contexts: in Excel formulas,
SUBTOTALis a function unrelated to any of this, and in chat or gaming "sub" means a subscription or a substitute player. Only in VBA does it mean subroutine.
Running and Debugging a Sub Safely
Press F8 to step through a Sub one line at a time; the highlighted line runs next. Press F5 to run the whole thing, and place the cursor inside the block first so VBA knows which Sub you mean. Breakpoints, set by clicking the grey margin beside a line, pause execution at that point so you can inspect values in the Immediate window with a question mark, for example ?Range("A1").Value.
Two habits prevent most trouble. Keep Subs short and give each one a single job, so a failure is easy to locate. And back up the workbook before running anything that writes or deletes data. Macros can be saved in .xlsm workbooks; a workbook saved as .xlsx drops the code.
Frequently Asked Questions
Is "sub" in VBA slang?
No. It is the literal keyword of the language, standing for subroutine. Microsoft documentation and every VBA reference use Sub as the formal term, so it is standard technical vocabulary, not informal shorthand.
Can a Sub return a value?
Not directly. Subroutines perform actions only. To pass a result back, use a Function and assign its return value to a variable, or write the result into a cell or a module-level variable, which is the less tidy option.
What is the difference between Sub and Private Sub?
Both define a subroutine. Public Subs appear in the macro list and can be called from other modules; Private Subs are hidden from the macro dialog and callable only from code in the same module. Event handlers such as Worksheet_Change must be Private by convention and sit in the relevant object module.
How do I run a Sub in Excel?
For a Public Sub in a standard module, press Alt+F8 to open the macro list and pick it, or assign it to a button through Assign Macro. From the VBA Editor, place the cursor inside the block and press F5.
Why is my macro not showing in the macro list?
The usual causes are that the Sub is Private, that it lives in a sheet or workbook module rather than a standard module, or that macro settings in the Trust Center are blocking it. Check the declaration line first, then the module location, then the security settings.
Can I use a Sub as a worksheet function?
No. Only Functions appear in Excel's function list and can be entered in a cell. Subs can be triggered from the macro list, a button, a keyboard shortcut, or other code, but not from a formula.