Delete module content

 2000-02-05    VBE    0    169

It's not possible to delete all kinds of modules, you can't delete the codemodules for worksheets, charts and ThisWorkbook. In these modules you have to delete the content instead of the module itself:

Sub DeleteModuleContent(ByVal wb As Workbook, ByVal DeleteModuleName As String)
' requires a reference to the Microsoft Visual Basic Extensibility library
' deletes the contents of DeleteModuleName in wb
' use this if you can't delete the module
    On Error Resume Next
    With wb.VBProject.VBComponents(DeleteModuleName).CodeModule
        .DeleteLines 1, .CountOfLines
    End With
    On Error GoTo 0
End Sub
DeleteModuleContent ActiveWorkbook, "Sheet1"