Excel表单:识别未使用的代码

Pat*_*rez 10 excel vba excel-vba

我正在更新别人写的应用程序(当然:)

我发现很多未使用的Sub CommandButtonXX_Click()潜艇,我不总是确定按钮是否仍然存在.有没有办法(程序,VBE接口,外部工具)做一些清理,同时避免删除仍在使用的代码?

属性框顶部的列表似乎确实可靠,因为它对上下文敏感:如果您在选项卡中,它只显示该选项卡的项目.

bre*_*tdj 10

一个有趣的问题!

  1. 我已经大大修改了Pearson的代码,列出了模块中的所有过程,以查找CommandButtonXX_Click每个工作表上的所有代码(不包括其他子代码),
  2. 然后尝试将每个CommandButtonXX_Click代码与该工作表上的实际按钮相匹配.
  3. 如果没有匹配,则删除该按钮,并Msgbox在末尾列出所有删除

编写VBA编辑器可能会有问题,因此请事先保存您的工作.我避免早期绑定Pearson使用的扩展性库.

[2012年10月4日:更新以使用UserForms而非Sheets]

       SConst vbext_ct_MSForm = 3
Sub ListProcedures()
    Dim VBProj
    Dim VBComp
    Dim CodeMod
    Dim LineNum As Long
    Dim NumLines As Long
    Dim ProcName As String
    Dim ObjButton
    Dim ProcKind
    Dim strBadButtons As String
    Set VBProj = ActiveWorkbook.VBProject
    For Each VBComp In VBProj.vbcomponents
        If VBComp.Type = vbext_ct_MSForm Then
            Set CodeMod = VBComp.CodeModule
            With CodeMod
                LineNum = .CountOfDeclarationLines + 1
                Do Until LineNum >= .CountOfLines
                    ProcName = .ProcOfLine(LineNum, 0)
                    If ProcName Like "CommandButton*_Click" Then
                        Set ObjButton = Nothing
                        On Error Resume Next
                        Set ObjButton = VBComp.Designer.Controls(Replace(ProcName, "_Click", vbNullString))
                        On Error GoTo 0
                        If ObjButton Is Nothing Then
                            strBadButtons = strBadButtons & CodeMod.Name & "-" & Replace(ProcName, "_Click", vbNullString) & vbNewLine
                            .DeleteLines .ProcStartLine(ProcName, 0), .ProcCountLines(ProcName, 0)
                        End If
                    End If
                    LineNum = LineNum + 1
                Loop
            End With
        End If
    Next
    If Len(strBadButtons) > 0 Then MsgBox "Bad Buttons deleted" & vbNewLine & strBadButtons
End Sub
Run Code Online (Sandbox Code Playgroud)

  • 太棒了!非常感谢.太糟糕了我不能接受它两次;-) (2认同)

aev*_*nko 6

有一个名为MZ-Tools的免费加载项工具,可用于识别未使用的过程(它可以做更多的事情).这是链接:http://www.mztools.com/v3/download.aspx