Skip to content

VBA361 — Executable statement at module level

Severity: 🔴 error Category: placement Phase: 3.5

Description

Code outside a Sub/Function/Property runs implicitly at module load — but VBA only permits declarative statements (Public/Private/Dim/Const/Type/Enum/Declare/Option/Implements/Attribute/DefXxx) in that position. A stray Debug.Print or assignment at module top is a hard compile error.

Failing example

Attribute VB_Name = "M"
Option Explicit
Debug.Print "this is illegal at module level"
Sub S(): End Sub

Compliant example

Attribute VB_Name = "M"
Option Explicit
Sub S()
    Debug.Print "executed when S runs"
End Sub

How to fix

Move the statement into a procedure body — Sub Main() is a common entry point, or wrap it in an Auto_Open / Workbook_Open event handler.


Source: src/rules.py — entry VBA361.