Không thể xóa tất cả các loại mô-đun, bạn không thể xóa mã hóa cho trang tính, biểu đồ và ThisWorkbook. Trong các mô-đun này, bạn phải xóa nội dung thay vì chính mô-đun:

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

Ví dụ:

DeleteModuleContent ActiveWorkbook, "Sheet1"