To be able to work with VBA in Excel, you must have basic knowledge of modules and macros. In the previous article, we shared with you the structure of a VBA, and in this article, let’s learn about what a module is in VBA and basic operations with modules.
Nội dung
1. What is a Module in Excel VBA?
Module is a component in the Project section of VBA, which is the place to store the Macros we have recorded such as: Sub, Function,… This is a basic component in almost every program with VBA.
In the VBA working window, you can find the Module section in the Project section:
Maybe when you open the VBA window in a new Excel file, you will not see the module section. But we have 2 different ways to create modules in VBA extremely simply.
- Method 1: Record any macro in VBA and the software will automatically create a new module for you.
- Method 2: Create a module directly using the Insert feature in the VBA window.
Right-click on any blank space in the VBA window, select Insert, then select Module to create a new module.
Please note that the names of modules in VBA will not be duplicated. If you want to know more about the notes when naming modules in VBA, please watch the video below.
2. Basic operations with modules in VBA Excel
Once you have a module in VBA Excel, there are some basic operations that you need to know:
Rename module
To rename a module, left-click on the default name that VBA has set for that module and then look down at the Properties window right below the Project window.
We recommend that you name the module instead of using the default name because this will help manage it more effectively. If you use the default name, it will be difficult to find out which module the macros, subs, and functions are saved in.
Module is an important and indispensable component of VBA. Therefore, mastering how to use this function is necessary to be good at VBA.
Delete a module
When you no longer need a module, you can delete it very simply as follows:
Right-click on the name of the module you want to delete and click Remove:
Then VBA will display the message: “Do you want to export Module1 before removing it?” meaning do you want to extract the content of this module before removing it?
This will help you if you accidentally delete a module containing an important code. Therefore, click Yes to save the content of that module to another location in case you need to use it. After you click Yes, the Export File window will open.
Save the content of the module you are about to delete in Basic Files (*.bas) format so that we can easily reload it into VBA later.
To reload the saved content into VBA, right-click on any empty space in the Project window and select Import File:
Then find the location where the file (*.bas) was saved => select the file => click Open to reload it into VBA. Even if you don’t delete a module in VBA, you can still Export it and load it into other files for use. This will help you have a VBA code library to work faster and more efficiently without having to rewrite it from scratch.
Through this article, you have learned the basic operations with modules in Excel. If you are new to learning, you should open VBA to practice to remember the knowledge.
Good luck!