To declare a global variable in VBA (Visual Basic for Applications), you need to use the Public keyword in the Declarations section of a module. This makes the variable accessible across all procedures and modules in the VBA project.
Steps:
1. Open the VBA editor (Alt + F11).
2. Insert a new module (Insert > Module) if none exists.
3. Declare the global variable at the top of the module, outside any procedures, using the Public keyword.
Example:
Public GlobalVar As String
Sub Example()
GlobalVar = “Hello, World!”
MsgBox GlobalVar
End Sub
In this example, GlobalVar is accessible from any procedure in the project.