Remove Modules ALL Workbboks

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,060
I have been playing around with scripts trying to delete modules from ALL workbooks but no matter what I try it only deletes them from the current active workbook, can someone please help.

I have read other forum threads on this site and Chip Pearsons but nothing seemes to work.

Before anyone asks, YES the module names are correct.


The first code I tried was.

Code:
 Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
 
        Set VBComps = ActiveWorkbook.VBProject.VBComponents
 
        For Each VBComp In VBComps
            Select Case VBComp.Type
                Case vbext_ct_StdModule
                    modName = VBComp.Name
                'If modName = "X_Ranges_And_Settings" Then
                    'VBComps.Remove VBComp
                  ' End If
                If modName = "X_Ranges_And_Settings1" Then
                    VBComps.Remove VBComp
                   End If
                If modName = "X_Ranges_And_Settings2" Then
                    VBComps.Remove VBComp
                   End If
 
            End Select
        Next VBComp

the next one I tried is

Code:
For Each Wb In Workbooks
    If Wb.Name <> "List1" Then
       Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
 
        Set VBComps = ActiveWorkbook.VBProject.VBComponents
 
        For Each VBComp In VBComps
            Select Case VBComp.Type
                Case vbext_ct_StdModule
                    modName = VBComp.Name
                'If modName = "X_Ranges_And_Settings" Then
                    'VBComps.Remove VBComp
                  ' End If
                If modName = "X_Ranges_And_Settings1" Then
                    VBComps.Remove VBComp
                   End If
                If modName = "X_Ranges_And_Settings2" Then
                    VBComps.Remove VBComp
                   End If
 
            End Select
        Next VBComp
 
 
    End If
Next Wb

I have also tried

Code:
Sub DeleteModule()
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
 
        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents("X_Ranges_And_Settings1")
        VBProj.VBComponents.Remove VBComp
    End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Look at the first line in your code.You are applying the code to the activeworkbook not to All workbooks.

Set VBComps = ActiveWorkbook.VBProject.VBComponents


Try this :

Code:
Option Explicit

Private Sub RemoveModule(ModuleName As String)
 
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent

    For Each VBProj In Application.VBE.VBProjects
    
        For Each VBComp In VBProj.VBComponents
        
            If VBComp.Name = ModuleName Then VBProj.VBComponents.Remove VBComp
        
        Next VBComp
    
    Next VBProj
 
End Sub
 
Sub Test()
 
    RemoveModule ModuleName:="X_Ranges_And_Settings"
    RemoveModule ModuleName:="X_Ranges_And_Settings1"
    RemoveModule ModuleName:="X_Ranges_And_Settings2"
    
End Sub
 
Upvote 0
Thanks for your reply Jaafer but it bugs out on the following line.

The error message says user defined type not defined.

Code:
Dim VBProj As VBIDE.VBProject
 
Upvote 0
You will need to add a reference to the VBA Exntensibility library or you can declare the valriables as Object like :

Code:
Option Explicit
 
Private Sub RemoveModule(ModuleName As String)
 
    Dim VBProj [COLOR=red]As Object[/COLOR]
    Dim VBComp [COLOR=red]As Object[/COLOR]
 
    For Each VBProj In Application.VBE.VBProjects
 
        For Each VBComp In VBProj.VBComponents
 
            If VBComp.Name = ModuleName Then VBProj.VBComponents.Remove VBComp
 
        Next VBComp
 
    Next VBProj
 
End Sub
 
Sub Test()
 
    RemoveModule ModuleName:="X_Ranges_And_Settings"
    RemoveModule ModuleName:="X_Ranges_And_Settings1"
    RemoveModule ModuleName:="X_Ranges_And_Settings2"
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,785
Messages
6,126,890
Members
449,347
Latest member
Macro_learner

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top