Delete module content using VBA in Microsoft Excel

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)
' 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"

Leave a Reply

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

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.