Open In App

How to Run Code from a Module in Excel VBA

Last Updated : 06 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

VBA Macro is for developers. In Excel, the macro is a piece of code written in VBA and VBA is Microsoft’s programming language, it stands for Visual Basic for Applications. The module is a file with a .bcf extension that stores the code written in the Visual Basic for Applications editor. Let’s learn, how to run a 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

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.

click-on-Module

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

Create-a-new-module

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

Add-code

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. 

Press-run-button

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. 

A-message-box-appear

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. 

Click-on-the-plus-to-create-a-new-worksheetNew-worksheet-is-created

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

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

Click-on-Run

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

A-dialogue-box-appears

FAQs on How to Run Code from a Module in Excel VBA

What is a VBA module in Excel?

A VBA module is a designated space within the Visual Basic for Applications(VAB) editor of Excel where you can write and store custom code. It allows you to create a manage your own script to automate tasks and enhance Excel’s functionality.

How to access the VBA editor in Excel?

To access the VBA editor, go to the “Developer” tab in the Excle’s ribbon. If the “Developer” tab is not visible, you can enable it by going to File >Options> Customize Ribbon, and then checking the “Developer” option.

Can we run VBA code directly from a module?

Yes, we can run VBA code directly from a module. Simply write the desired code within the module and use the “Run” button in the Tools bar or press “F4″( or “Fn +F4”) on your keyboard to execute the code.

Can we execute multiple lines of code in a single module?

Yes, we can write and execute multiple lines of code within a single module. You can create a subroutines, functions, and procedures that consist of multiple lines of code to achieve specific tasks.

Are there any limitations to the scope of a VBA module in Excel?

The scope of a VBA module is worksheet-level, meaning you can use the written modules in different worksheets within the same workbook. However, the code within a module may not be accessible or applicable across different workbooks.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads