Global scope in Excel VBA means a variable or procedure can be used from any module in the workbook, not just the one where it was declared. You achieve it with the Public keyword at the top of a standard module (for variables) or by declaring a Sub or Function as Public (which is the default for procedures in standard modules). The shortest working recipe: insert a standard module, put Public myVariable As String above any procedure, then read or write myVariable from any other module in the same project.

What You'll Need

  • Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook saved as .xlsm (macro-enabled) so your code persists.
  • Basic familiarity with the VBA editor (Alt+F11 on Windows, Option+F11 on Mac).

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). The Project Explorer on the left lists your workbook, its sheets, ThisWorkbook, and any modules. Labels can differ slightly by version, but the layout is consistent.
  2. Insert a standard module. In the Project Explorer, right-click your workbook name, choose Insert > Module. A new folder called Modules appears with Module1. Do not put global variables in a sheet module or ThisWorkbook unless you specifically want them scoped to that object.
  3. Declare a public variable. At the very top of Module1, above any Sub or Function, type:
    Public gUserName As String
    Public gCounter As Long
    The Public keyword is what makes the scope global. Dim at module level makes it private to that module only.
  4. Optionally force explicit declarations. At the top of the module, add Option Explicit so every variable must be declared. This prevents typos that silently create new local variables. You can also set Require Variable Declaration under Tools > Options > Editor.
  5. Write a procedure that uses the variable. In the same module:
    Public Sub SetName()
    gUserName = "Alex"
    End Sub
  6. Read it from another module. Insert a second module (Insert > Module) and add:
    Public Sub ShowName()
    MsgBox gUserName
    End Sub
    Run SetName first, then ShowName. The message box shows Alex, proving the variable is visible across modules.
  7. Make procedures public. In a standard module, Sub and Function are Public by default, so they are callable from other modules and from the Macros dialog. Writing Public Sub MyMacro() is explicit and harmless. To hide a helper, use Private Sub.
  8. Test scope boundaries. Add a second variable with Dim localOnly As String at module level in Module1. Try to reference localOnly from Module2. VBA throws a "Variable not defined" compile error under Option Explicit, confirming the difference between module-level and global scope.
  9. Save and reopen. Save as .xlsm, close, and reopen. Public variables reset to their default values (empty string, zero) when the project is reset or the workbook is reopened, because VBA does not persist variable values between sessions. If you need persistence, store values in a worksheet cell, a named range, or the registry via SaveSetting.

Common Mistakes

  • Putting Public declarations inside a Sub or Function. That is a syntax error; declarations belong at module level, above all procedures.
  • Using Dim instead of Public at module level and expecting other modules to see the variable. Dim at module level is private to that module.
  • Declaring the same public name in two modules. This causes an ambiguous name compile error; keep one declaration in one place.
  • Assuming a public variable keeps its value after the code stops, the project is reset (Run > Reset), or the workbook closes. It does not.
  • Declaring public variables in a sheet module or ThisWorkbook and then wondering why other modules cannot see them. Use a standard module.
  • Forgetting Option Explicit, then misspelling a global name and silently creating a new local variable instead.

Tips

  • Prefix global variables (for example g or pub) so readers can tell them apart from locals at a glance.
  • Prefer a small number of well-named globals, or a single Public Type (user-defined type) that groups related settings.
  • Use Public Const for values that never change; constants cannot be accidentally overwritten.
  • If you need a value to survive closing the workbook, write it to a hidden worksheet or use SaveSetting/GetSetting rather than relying on a public variable.
  • Keep all public declarations in one dedicated module (often called modGlobals) so they are easy to find and audit.

Troubleshooting

"Variable not defined" compile error. Check that the variable is declared Public in a standard module, that Option Explicit is on, and that the spelling matches exactly.

"Ambiguous name detected." The same public name is declared in more than one module. Delete the duplicate.

Value is empty when read from another module. The setting procedure probably did not run first, or the project was reset. Run the setter, then the reader, without pressing Reset in between.

Public variable not visible at all. Confirm it is in a standard module (Modules folder), not in Sheet1, ThisWorkbook, a class module, or a UserForm, where the rules differ.

Macro does not appear in the Macros dialog. It may be Private, or it may have arguments. Public Subs with no arguments appear; Functions generally do not appear in the macro list.

Values lost after reopening. This is expected. Move persistent data to a worksheet cell or use SaveSetting.

Frequently Asked Questions

What is the difference between Public and Dim at module level?

Public makes the variable visible to every module in the project. Dim at module level makes it visible only inside the module where it is declared.

Can I declare a global variable inside a Sub?

No. Public declarations must sit at module level, above all procedures. Placing one inside a procedure causes a compile error.

Do public variables keep their values after the workbook closes?

No. They reset when the project resets or the workbook reopens. Store persistent values in a worksheet cell, a named range, or with SaveSetting.

Are Subs and Functions global by default?

In standard modules, yes. They are Public unless you mark them Private. In sheet modules, ThisWorkbook, class modules, and UserForms, the default visibility is different.

Can I use a public variable across workbooks?

Not directly. Public scope is limited to the VBA project (the workbook) where it is declared. To share data between workbooks, use a worksheet cell, a named range, or an add-in.

Why does my public variable show as empty in another module?

The procedure that assigns it has not run yet, or the project was reset. Run the setter first, then the reader, without resetting in between.