反向工程VBA代码Excel

4

我不是VBA程序员,但是我需要将某人的VBA代码重新实现成另一种语言,这是一项很不愉快的任务。该VBA代码由75个模块组成,使用一个巨大的“计算表”来存储所有“全局变量”。因此,它通常使用不太具有描述性的变量名称,而不是使用描述性的变量名称:

= Worksheets("bla").Cells(100, 75).Value

或者

Worksheets("bla").Cells(100, 75).Value =

更糟的是,“计算表”中还包含一些公式。

有没有(免费)工具可以让您反向工程这样的代码(例如创建Nassi-Shneiderman图、流程图)?谢谢。


哎呀,我希望这个发起人已经不在这个组织工作了 - 或者任何其他组织都不工作!很抱歉,我想不出任何有用的东西。也许如果每个变量单元格在许多地方都被使用,你可以导出代码,然后编写一些正则表达式替换值的内容 - 但我不确定这是否有帮助。 - Julian Knight
谢谢,我也在考虑类似的事情。 - cs0815
1个回答

4
我认为@JulianKnight的建议应该可行。在此基础上,您可以:
  1. Copy all the code to a text editor capable of RegEx search/replace (Eg. Notepad++).
  2. Then use the RegEx search/Replace with a search query like:
    Worksheets\(\"Bla\"\).Cells\((\d*), (\d*)\).Value
  3. And replace with:
    Var_\1_\2

    This will convert all the sheet stored values to variable names with row column indices.
    Example:

    Worksheets("bla").Cells(100, 75).Value    To    Var_100_75
    
  4. These variables still need to be initialized.
    This may be done by writing a VBA code which simply reads every (relevant) cell in the "Bla" worksheet and writes it out to a text file as a variable initialization code.
    Example:

    Dim FSO As FileSystemObject 
    Dim FSOFile As TextStream 
    Dim FilePath As String 
    Dim col, row As Integer 
    
    FilePath = "c:\WriteTest.txt" ' create a test.txt file or change this
    
    Set FSO = New FileSystemObject 
     ' opens  file in write mode
    Set FSOFile = FSO.OpenTextFile(FilePath, 2, True) 
     'loop round adding lines
    For col = 1 To Whatever_is_the_column_limit
        For row = 1 To Whatever_is_the_row_limit
         ' Construct the output line
            FSOFile.WriteLine ("Var_" & Str(row) & "_" & Str(col) & _
                           " = " & Str(Worksheets("Bla").Cells(row, col).Value)) 
        Next row
    Next col
    
    FSOFile.Close 
    

显然,您需要针对其他语言进行输出行语法和变量名称结构的纠正。

P.S. 如果您不熟悉正则表达式(RegEx),您将在网络上找到大量解释它的文章。


http://www.experts-exchange.com/articles/Programming/Languages/Visual_Basic/Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html - Julian Knight

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接