10

是否可以使用 VBA 从 Excel 文件中删除所有 VBA 模块?

在运行此脚本之前,模块的名称(如果它们存在的话)是未知的。

4

2 回答 2

13

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).

于 2013-08-29T18:47:55.107 回答
1

这是一个类似的替代方案,它只删除了 ClassModules:

On Error Resume Next
With wbk.VBProject
    For x = .VBComponents.Count To 1 Step -1
        If .VBComponents(x).Type = vbext_ct_StdModule Then
            .VBComponents.Remove .VBComponents(x)
        End If
    Next x
End With
On Error GoTo 0
于 2013-08-29T19:00:37.437 回答