How to Delete a Module in Excel VBA?
Have you ever seen a text file in a notepad or a source code file in visual studio code? These all are just basic files where you can write some code or information. Similarly, we have modules in excel, each module has its code window, where you can write some code and allow it to automate your repetitive tasks. There can be multiple modules. We can create, rename or delete a module in just a few mouse clicks. In this article, we will learn how to create and delete a module in excel VBA.
Module and its types
A module is a set of instructions written in a VBA editor. There are broadly 5 types of modules in VBA:
- General-Purpose Module: It is the simplest type of module. If we record a macro that will get stored in a module. This is called a general-purpose module. We can simply write code in the VBA editor also.
- Workbook Module: Workbook module handles the events happening at a workbook level. For example, opening, closing or renaming a workbook.
- Worksheet Module: The worksheet module is driven by the events happening at the worksheet level. For example, activating a worksheet or flipping between different worksheets. Automating the task such as merging the worksheets.
- Userform Module: Userform Module is a way to input a piece of information from the user. It is one of the most useful tools of VBA.
- Class Module: It’s similar to the classes in other languages. We create our objects. VBA has built-in objects such as the Collection, Workbook, etc. The inheritance in VBA is minimal and much more restricted than in other languages.
Creating a VBA module
Following are the steps to create a VBA module,
Step 1: Go to Developer Tab, and select Visual Basic in the menu bar.
Step 2: A VBA editor appears. We can see the information on all the worksheets and workbooks in the editor tab.
Step 3: Go to Insert Tab and select Module.
Step 4: A module name module1 is created.
Deleting a VBA module
Following are the steps to delete a VBA module,
Step 1: Given 3 modules name module1, module2, and module3. Delete module3 from the Modules.
Step 2: Right-click on module3 and click on Remove module3. A dialogue box appears.
Step 3: The dialogue box tells whether you want to export the given module or not. If you, do not want to save the module on your PC, then click on No.
Step 4: Module3 has been deleted from the VBA editor.