Delete a module using VBA in Microsoft Excel


In this article we will create a macro to delete module from the workbook.

We will be using the MainModule as raw module.


We want to get rid of this MainModule.

Click on this button for the same.



Logic explanation

In this article, we have created two macros, DeleteVBComponent and calling_procedure.


It takes the workbook name and module name as inputs, and deletes the module from the workbook.


It is used to call the DeleteVBComponent macro with the active workbook and MainModule as inputs.

Code explanation

Application.DisplayAlerts = False

This code is used to disable the alert message.

wb.VBProject.VBComponents.Remove wb.VBProject.VBComponents(CompName)

This code is used to remove the module from the workbook.

Application.DisplayAlerts = True

This code is used to enable the alert message.


Please follow below for the code

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


If you liked this blog, share it with your friends on Facebook. Also, you can follow us on Twitter and Facebook.

We would love to hear from you, do let us know how we can improve our work and make it better for you. Write to us at


  1. Richard Sellers

    Does this method no long work? I'm trying to use it in Excel 2016 and get a Runtime error 1004, application-defined or object-defined error.

    Here is the code:
    ActiveWorkbook.VBProject.VBComponents.Remove ActiveWorkbook.VBProject.VBComponents("modTimesheetTools")

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.