您如何分析VBA代码以提取相关项目,例如可变名称,工作簿参考和指向各种驱动器的链接?

发布于 2025-02-07 05:01:31 字数 1774 浏览 0 评论 0原文

因此,我一直在使用一个修改后的脚本,以从具有大量VBA模块的工作簿中提取重要信息,我需要分解和理解。如何修改下面的脚本以返回在VBA模块中找到关键字的整个行?

例如,如果我将“ What whyt”设置为“ Workbooks.s.s.open” - 如何在VBA代码中返回整个行,以便我确切知道子例程打开的工作簿(不必手动扫描代码?

另外,请说 ?我想通过30多个关键字(DIM,Workbook,Drive等)保存在Excel范围内,我该如何添加一个额外的循环来测试整个项目中的其他关键字

?在工作簿中的每个模块中,一个变量,工作簿,功能和子例程 - 然后将整个行从VBA代码返回到一个分析的工作簿中。谢谢。

Sub Find_Keywords()

Row = 1
Dim Vbc As VBComponent
Dim Line_Number, Start_Line, Line_Count As Long
Dim Line, Temp_Procedure_Name, Final_Procedure_Name, SubOrFun As String, Pk As vbext_ProcKind
Dim wb as Workbook
Dim Found as Boolean
Dim findWhat As String

Pk = vbext_pk_Proc
Set wb = Workbooks.Open("Module Workbook.xlsx")
findWhat ="Workbooks.Open"

  
    
    For Each Vbc In ThisWorkbook.VBProject.VBComponents
        Line_Number = 1
        Temp_Procedure_Name = ""
        
            For Line_Number = 1 To Vbc.CodeModule.CountOfLines
            Line = Vbc.CodeModule.Lines(Line_Number, 1)
            
            Final_Procedure_Name = Vbc.CodeModule.ProcOfLine(Line_Number, Pk)

                Temp_Procedure_Name = Final_Procedure_Name
        Found = Vbc.CodeModule.Find(Target:=findWhat,StartLine:=Vbc.CodeModule.ProcStart(Final_Procedure_Name,Pk), StartColumn:=1, EndLine:=Vbc.CodeModule.ProcCountLines(Find_Procedure_Name, Pk), EndColumn:=255, WholeWorld:=False,MatchCase:=False,PatternSearch:=False)

                If Found Then
                ThisWorkbook.Sheets(1).Cells(Row, 1).Value = Vbc.Name
                ThisWorkbook.Sheets(1).Cells(Row, 2).Value = Final_Procedure_Name
                ThisWorkbook.Sheets(1).Cells(Row, 3).Value = findWhat
             
                
                Row = Row + 1
                End If
            
                Next
        Next
End Sub

so I've been working with a modified script to extract important information from a workbook with a ton of VBA modules that I need to breakdown and understand. How can I modify the script below to return the entire line where the keyword was found in the VBA module?

For example, if I set "findWhat" to "Workbooks.Open" -- how can I return the entire line in the VBA code so I know exactly what workbook the subroutine is opening (without having to scan the code manually?

Additionally, say I wanted to loop through 30+ keywords (Dim, Workbook, Drive, etc.) saved in an Excel range, how could I add an additional loop to test out additional keywords in the entire project?

I'd like to extract every reference to a variable, workbook, function, and subroutine within every module in the workbook -- and then return the entire line from the VBA code into a workbook for analysis. There are hundreds of modules here so this will hopefully save me a ton of time. Thank you.

Sub Find_Keywords()

Row = 1
Dim Vbc As VBComponent
Dim Line_Number, Start_Line, Line_Count As Long
Dim Line, Temp_Procedure_Name, Final_Procedure_Name, SubOrFun As String, Pk As vbext_ProcKind
Dim wb as Workbook
Dim Found as Boolean
Dim findWhat As String

Pk = vbext_pk_Proc
Set wb = Workbooks.Open("Module Workbook.xlsx")
findWhat ="Workbooks.Open"

  
    
    For Each Vbc In ThisWorkbook.VBProject.VBComponents
        Line_Number = 1
        Temp_Procedure_Name = ""
        
            For Line_Number = 1 To Vbc.CodeModule.CountOfLines
            Line = Vbc.CodeModule.Lines(Line_Number, 1)
            
            Final_Procedure_Name = Vbc.CodeModule.ProcOfLine(Line_Number, Pk)

                Temp_Procedure_Name = Final_Procedure_Name
        Found = Vbc.CodeModule.Find(Target:=findWhat,StartLine:=Vbc.CodeModule.ProcStart(Final_Procedure_Name,Pk), StartColumn:=1, EndLine:=Vbc.CodeModule.ProcCountLines(Find_Procedure_Name, Pk), EndColumn:=255, WholeWorld:=False,MatchCase:=False,PatternSearch:=False)

                If Found Then
                ThisWorkbook.Sheets(1).Cells(Row, 1).Value = Vbc.Name
                ThisWorkbook.Sheets(1).Cells(Row, 2).Value = Final_Procedure_Name
                ThisWorkbook.Sheets(1).Cells(Row, 3).Value = findWhat
             
                
                Row = Row + 1
                End If
            
                Next
        Next
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文