How to Run Code from a Module in Excel VBA
Last Updated :
09 Sep, 2025
VBA macros are tools designed for developers to automate tasks in Excel. A macro is a piece of code written in VBA (Visual Basic for Applications), which is Microsoft’s programming language for automating Office applications. A module is a container within the VBA editor where this code is stored (typically with a .bas
extension, not .bcf
, so this might need correction). Let’s learn how to run code from a module in Excel VBA.
Run a code from the Module in Excel VBA
Step 1: Go to the Developer Tab, Under the code section you will find Visual Basic. Now click on Visual Basic.

Step 2: Microsoft Visual Basic for Applications(VBA) dialogue box appears. In the Menu bar, go to Insert Tab, and click on Module.

Step 3: A Module named Module1 is created under the VBA project.

Step 4: Write the code you want to run in Microsoft excel. For example, a message box will appear stating "Learning geeks for geeks".

Step 5: In the Tools bar. Click on the run button. You can also press F4(Fn + F4) on your keyboard to run the module code.

Step 6: The code pauses and the Microsoft VBA editor minimizes and a message box appears in the MS Excel worksheet. Click Ok. The code resumes and the Microsoft VBA editor maximizes again.

Scope of Module in VBA
The scope of the module in VBA is workbook level. You can use the written modules in different worksheets. You have created a module named module1 in shee1 above. A sub-procedure named geeksforgeeks() is also created. We will use this procedure to check the scope of a module in VBA. Following are the steps:
Step 1: Click on the plus and a new worksheet named sheet2 is created.


Step 2: Go to the Developer tab, and click on Macros.

Step 3: A dialogue box named Macro appears. In the macro name, you will find a procedure name geeksforgeeks(). This was the procedure that we wrote in sheet1 of this workbook. This proved that the scope of the module is workbook level. Select it, and click on Run.

Step 4: The code written in the module1 sub procedure gets executed. A dialogue box appears.

Conclusion
Running VBA code from a module in Excel allows you to reuse code across worksheets and streamline repetitive tasks. With just a few steps, you can create, store, and execute macros from the module, improving both efficiency and accuracy in your Excel projects.