Delete a specific module by vba whithout the use of for each

drom

Active Member
Joined
Mar 20, 2005
Messages
498
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
  6. 2007
Hi and Thanks in advance!!

if I use the following macro I can delete all the module etc from a specific Workbook.


'
Code:
Sub Delete_AllVBA()
  On Error Resume Next
Dim WkbName As String: WkbName = "c\:gvjvcjhgsdf\vfdmnbvv\Hello.xls" 'ThisWorkbook.Name
Dim VBComp As VBIDE.VBComponent
Dim VBComps As VBIDE.VBComponents:  Set VBComps = Workbooks(WkbName).VBProject.VBComponents
  For Each VBComp In VBComps
    Select Case VBComp.Type
      Case vbext_ct_StdModule:        VBComps.Remove VBComp    '1:  "Standard Module"
      Case vbext_ct_ClassModule:      VBComps.Remove VBComp    '2:  "Class Module"
      Case vbext_ct_MSForm:           VBComps.Remove VBComp    '3:  "MS Form"
      Case vbext_ct_ActiveXDesigner:  VBComps.Remove VBComp    '11: "ActiveX Designer"
      Case vbext_ct_Document:         'VBComps.Remove VBComp   '100:"Document"
        With VBComp.CodeModule
          If .CountOfLines > 0 Then
            .DeleteLines 1, .CountOfLines
          End If
        End With
    End Select
  Next VBComp
  Beep
End Sub

'


but is there a way of deleting a specific module whithout the use of For Each VBComp In VBComps


I mean some thing like:

Activeworkbook.VBComponents("Module 1").remove

if "module 1" is the module I want to delete

Thanks!

PS: I know the following web

HTML:
 http://www.cpearson.com/excel/vbe.aspx
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Code:
Sub TestMR()
On Error Resume Next
With ActiveWorkbook.VBProject
    For x = .VBComponents.Count To 1 Step -1
        If .VBComponents(x).Name = "Module1" Then
            .VBComponents.Remove .VBComponents(x)
        End If
    Next x
End With
End Sub
 
Upvote 0
This presumes you're deleting a module from the wb this code if housed in (ThisWorkbook).

Code:
Sub KillMod()
    ThisWorkbook.VBProject.VBComponents.Remove _
        ThisWorkbook.VBProject.VBComponents("Module2")
End Sub

Mark
 
Upvote 0

Forum statistics

Threads
1,202,966
Messages
6,052,846
Members
444,603
Latest member
dustinjmangum

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