Obviously you can. The following code will complete the task:
Sub compact_code() On Error Resume Next Dim Element As Object For Each Element In ActiveWorkbook.VBProject.VBComponents ActiveWorkbook.VBProject.VBComponents.Remove Element Next End Sub
This will delete all modules, including ClassModules and UserForms, but save all object modules (sheets, workbook).
Kazimierz Jawor
source share