Delete VBA Module

Douglas

New Member
Joined
Feb 18, 2002
Messages
19
I need to write a piece of code which will open a workbook and delete one of it's VBA modules.

Any suggections?
Thanks
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You need to set a reference to the VBIDE in Tools-References from the VB editor (the name is "Microsoft Visual Basic X.X Extensibility"). Then you can do something like this:

Code:
Sub DelModule()

    Dim VBP As Object
    
    Set VBP = ActiveWorkbook.VBProject
    
    VBP.VBComponents.Remove (VBP.VBComponents("Module2"))
    Set VBP = Nothing
End Sub

HTH,

Russell
 
Upvote 0
Great - thanks.

Do I use a silimar method is I just decide to delete a Sub routine within a module?
 
Upvote 0
Nope use something like what Chip has created:

Sub DeleteProcedure()

Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim HowManyLines As Long

Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("NewModule").CodeModule
With VBCodeMod
StartLine = .ProcStartLine("MyNewProcedure", vbext_pk_Proc)
HowManyLines = .ProcCountLines("MyNewProcedure", vbext_pk_Proc)
.DeleteLines StartLine, HowManyLines
End With

End Sub

http://www.cpearson.com/excel/vbe.htm

Define the module and procedure name.

HTH. Cheers, Nate
 
Upvote 0
all i could come up with was

Sub DeleteProcedureWB()

DisplayAlerts = False
Dim VBCodeMod As Object
Dim StartLine As Long

Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
With VBCodeMod
StartLine = (3)
.Deletelines StartLine
End With
ActiveWorkbook.Save
End Sub

it works but can u specify text to delete?
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,949
Members
448,534
Latest member
benefuexx

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