Fix VBA Compile Error: Invalid Outside Procedure
Question details
Understand and fix the VBA compile error caused by executable code outside a procedure.
- Product
- Microsoft Office Visual Basic for Applications (VBA)
- Device & OS
- not provided
- Scenario
- A user is editing or compiling VBA code in an Office module.
- Observed behavior
- The compiler reports “Invalid outside procedure” when it encounters an executable statement outside a Sub or Function.
Open the affected Office file and locate the module containing the error in the Visual Basic Editor. Identify any executable statements between procedure definitions or above the first procedure.
Move executable statements into a VBA procedure
Use this fix when an assignment or other executable statement appears outside a Sub or Function.
Module-level declarations, such as Option Explicit, variables, and constants, can remain outside procedures. Statements that perform actions must be inside an appropriate procedure.
In the Office application, select Developer > Visual Basic, then open the affected module in the Project Explorer.
Look for executable code above the first procedure or between an End Sub or End Function line and the next procedure. For example, StartDate = Now cannot appear there.
Move the statement into an existing Sub or create one. For example: Dim StartDate As Date Sub SetStartDate() StartDate = Now End Sub
In the Visual Basic Editor, select Debug > Compile VBAProject. If another error is highlighted, review the statement and its surrounding Sub or Function boundaries.

Work with Office files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface and support for common Microsoft Office formats, making it easier to move everyday document, spreadsheet, and presentation work. It does not fix errors in the Microsoft VBA compiler.

Frequently Asked Questions
What does “Invalid outside procedure” mean in VBA?
It usually means an executable statement, such as StartDate = Now, appears outside a Sub or Function. Move the statement inside the appropriate procedure.
Can I declare variables outside a VBA procedure?
Yes. You can place module-level declarations, such as Dim StartDate As Date, outside procedures. Assign a value using an executable statement inside a procedure.
Why does code between two procedures cause this error?
A statement after End Sub or End Function and before the next procedure is not part of either procedure. Move it above the appropriate ending line or into a new Sub or Function.
How can I check whether the VBA project compiles?
In the Visual Basic Editor, select Debug > Compile VBAProject. The editor will highlight a problem if it finds another compile error.




