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.

ArrowMainModule

We want to get rid of this MainModule.

Click on this button for the same.

ArrowMain

ArrowOutput

Logic explanation

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

DeleteVBComponent

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

calling_procedure

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 info@exceltip.com

Users are saying about us...

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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.

Visit Us On TwitterVisit Us On FacebookVisit Us On Google PlusVisit Us On Youtube