How To Delete a module using VBA in Microsoft Excel?
Tags: todeleteamodule, vbamacros
Published: 2021-10-12 01:52:28

---
title: How To Delete a module using VBA in Microsoft Excel?
published: true
date: 2021-10-11 09:21:05 UTC
tags: ToDeleteAModule,VBAMacros
canonical_url: https://geekexcel.com/how-to-delete-a-module-using-vba-in-microsoft-excel/
---

In this tutorial, you will learn how to  **delete a module**   **using VBA ** in Microsoft Excel. Let’s see them below!! Get the official version of  **MS Excel**  from the following link: [https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)

## Example

- Firstly, you need to **use the MainModule** as raw module to **create a macro button** in Excel.

![](https://geekexcel.com/wp-content/uploads/2021/10/Sample-data-20.png)<figcaption>Sample data</figcaption>

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you need to  **copy and paste the code**  given below.

```
Option Explicit
Sub DeleteVBComponent(ByVal wb As Workbook, ByVal CompName As String)
'Disabling the alert message
Application.DisplayAlerts = False
'Ignore errors
On Error Resume Next
'Delete the component
wb.VBProject.VBComponents.Remove wb.VBProject.VBComponents(CompName)
On Error GoTo 0
'Enabling the alert message
Application.DisplayAlerts = True
End Sub
Sub calling_procedure()
    'Calling DeleteVBComponent macro
    DeleteVBComponent ActiveWorkbook, "MainModule" 
End Sub

```

- You have to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/10/Save-the-Code-32.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- Then, you need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://geekexcel.com/wp-content/uploads/2021/10/Run-the-Code-27.png)<figcaption>Run the Code</figcaption>

- Finally, after  **running the macro** , a  **MainModule**  will get deleted from the VBA.

![](https://geekexcel.com/wp-content/uploads/2021/10/Output-19.png)<figcaption>Output</figcaption>

## Sum-Up

In the above short tutorial, we have illustrated ** ** how to  **delete a module using VBA**  in Microsoft Excel. Hope you like this article. Please feel free to state your  **query**  or  **feedback**  for the above article. Thank you so much for Reading!! To learn more, check out **[Geek Excel](https://geekexcel.com/)!! **And [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Further Reference:**

- **[How to Change Row Height in Microsoft Excel 2007?](https://geekexcel.com/how-to-change-row-height-in-microsoft-excel-2007/)**
- **[Find and Select Merged Cells in Excel Office 365](https://geekexcel.com/how-to-find-and-select-merged-cells-in-excel-office-365/)**
- **[How to Change a Row Height in Microsoft Excel 365?](https://geekexcel.com/how-to-change-a-row-height-in-microsoft-excel-365/)**
- **[Set Row Height or Column Width in Inches/Pounds/Centimeters/Pixels!!](https://geekexcel.com/set-row-height-or-column-width-in-inches-pounds-centimeters-pixels/)**
- **[How to Filter Merged Cells in Microsoft Excel 365?](https://geekexcel.com/how-to-filter-merged-cells-in-microsoft-excel-365/)**