Is it possible to remove all VBA modules from an Excel file using VBA?
The names of the modules if they exist at all are unknowns before running this script.
Obviously, you can. The following code will do the job:
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 remove all modules including ClassModules and UserForms but keep all object modules (sheets, workbook).