如何在Excel中找到任何宏或VBA?

时间:2018-01-15 21:39:43

标签: excel vba excel-vba

最近接管了一家公司的新职位,我今天收到了attached document。似乎附加的VBA或宏在我打印时会改变格式。有没有办法找到那些?我唯一能找到的是2个模块,用于保护和取消保护工作表。

3 个答案:

答案 0 :(得分:3)

Visual Basic编辑器的 Project Explorer 仅显示模块,这使得知道什么编码在哪里,特别是在较大的项目中或在具有工作簿的工作簿中非常繁琐。许多工作表 - 代码可以潜伏在任何地方!

我管理Rubberduck开源项目 - 我们构建一个VBIDE加载项,增强了VBE的导航工具(以及其他一些东西) - 例如 Code Explorer 允许您深入到程序级别,任何包含代码的模块都会有一些"展开"箭头:

Rubberduck's Code Explorer drilling down to member level

让您更容易看到哪些模块包含代码,一目了然。

答案 1 :(得分:1)

听起来像是由BeforePrint事件触发的工作簿宏。在找到模块的同一位置检查ThisWorkbook选项卡。

答案 2 :(得分:0)

如果您不希望(或者您不被允许)安装任何第三方软件(MZ-ToolsRubberduck会减轻您的工作),您可以考虑添加以下代码:附加模块并添加此库 - " Microsoft Visual Basic for Applications Extensibility 5.3库":

Option Explicit

Private strSubsInfo As String

Public Sub GetFunctionAndSubNames()

    Dim item            As Variant

    strSubsInfo = ""

    For Each item In ThisWorkbook.VBProject.VBComponents
        If ComponentTypeToString(vbext_ct_StdModule) = "Code Module" Then
            ListProcedures item.Name, False
            'Debug.Print item.CodeModule.lines(1, item.CodeModule.CountOfLines)
        End If
    Next item

    Debug.Print strSubsInfo

End Sub

Private Sub ListProcedures(strName As String, Optional blnWithParentInfo = False)

    'Microsoft Visual Basic for Applications Extensibility 5.3 library

    Dim VBProj          As VBIDE.VBProject
    Dim VBComp          As VBIDE.VBComponent
    Dim CodeMod         As VBIDE.CodeModule
    Dim LineNum         As Long
    Dim ProcName        As String
    Dim ProcKind        As VBIDE.vbext_ProcKind

    Set VBProj = ActiveWorkbook.VBProject
    Set VBComp = VBProj.VBComponents(strName)
    Set CodeMod = VBComp.CodeModule

    With CodeMod
        LineNum = .CountOfDeclarationLines + 1

        Do Until LineNum >= .CountOfLines
            ProcName = .ProcOfLine(LineNum, ProcKind)

            If blnWithParentInfo Then
                strSubsInfo = strSubsInfo & IIf(strSubsInfo = vbNullString, vbNullString, vbCrLf) & strName & "." & ProcName
            Else
                strSubsInfo = strSubsInfo & IIf(strSubsInfo = vbNullString, vbNullString, vbCrLf) & ProcName
            End If

            LineNum = .ProcStartLine(ProcName, ProcKind) + .ProcCountLines(ProcName, ProcKind) + 1
        Loop

    End With

End Sub

Function ComponentTypeToString(ComponentType As VBIDE.vbext_ComponentType) As String

    Select Case ComponentType

        Case vbext_ct_ActiveXDesigner
            ComponentTypeToString = "ActiveX Designer"

        Case vbext_ct_ClassModule
            ComponentTypeToString = "Class Module"

        Case vbext_ct_Document
            ComponentTypeToString = "Document Module"

        Case vbext_ct_MSForm
            ComponentTypeToString = "UserForm"

        Case vbext_ct_StdModule
            ComponentTypeToString = "Code Module"

        Case Else
            ComponentTypeToString = "Unknown Type: " & CStr(ComponentType)

    End Select

End Function

因此,您可以在即时窗口中打印所有函数和子函数。

GitHub with credits to CPearson