vba 代码重构 - 是否有任何工具可以提供帮助?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12725195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:59:20  来源:igfitidea点击:

vba code refactoring - are there any tools to assist?

vbaexcel-vbaexcel

提问by Siraj Samsudeen

I am trying to refactor my VBA code. I am so used to using refactoring in Java-based IDEs for a number of years. Does VBA editor support any refactoring or are there any add-ins? MZ Tools did not have any such functionality.

我正在尝试重构我的 VBA 代码。多年来,我非常习惯在基于 Java 的 IDE 中使用重构。VBA 编辑器是否支持任何重构或是否有任何加载项?MZ Tools 没有任何此类功能。

I want to be able to do at least the following: 1. Rename variables 2. Split Procedures into sub-procedures to make the code more readable 3. Change the scope of the variable from global to procedure and vice-versa

我希望至少能够执行以下操作:1. 重命名变量 2. 将过程拆分为子过程以使代码更具可读性 3. 将变量的范围从全局更改为过程,反之亦然

回答by Mathieu Guindon

Disclaimer: I'm heavily involved with this project.

免责声明:我参与了这个项目。



Rubberduckis an open-source add-in for the VBA/VB6 IDE under [very] active development, that includes this functionality.

Rubberduck是 VBA/VB6 IDE 的开源插件,正在 [非常] 积极的开发中,包括此功能。

Version 1.3 includes a Renamerefactoring:

1.3 版包括重命名重构:

rename refactoring

重命名重构

Version 2.0 (beta available, still stabilizing) includes a dozen refactorings:

2.0 版(测试版可用,仍在稳定中)包括十几个重构:

Rubberduck 2.0 refactorings

Rubberduck 2.0 重构

  • Introduce Parameterpromotes a local variable to a parameter
  • Introduce Fieldpromotes a local variable to module scope
  • Encapsulate Fieldturns a public field into a property
  • Move Closer to Usagemoves a field that's only used in 1 procedure, into that procedure. Or moves a local variable immediately above its first use.
  • Extract Interfacelets you pick what class members to extract into an interface, creates a new class modules with stubs for them, and makes the original class implement the extracted interface.
  • Implement Interfacecreates stubs for all members of an unimplemented interface, so you don't need to create them manually by selecting them one by one in the code pane dropdown:

    Implements IClass1
    
    Public Sub IClass1_DoSomething()
        Err.Raise 5 'TODO implement interface member
    End Sub
    
    Public Function IClass1_GetFoo() As Integer
        Err.Raise 5 'TODO implement interface member
    End Function
    
    Sub DoSomething()
    
    End Sub
    
    Function GetFoo() As Integer
    
    End Function
    
  • Introduce Parameter将局部变量提升为参数
  • Introduce Field将局部变量提升到模块作用域
  • Encapsulate Field将公共字段变成属性
  • Move Closer to Usage将仅在 1 个过程中使用的字段移动到该过程中。或者将一个局部变量直接移动到它第一次使用的上方。
  • Extract Interface允许您选择将哪些类成员提取到接口中,为它们创建带有存根的新类模块,并使原始类实现提取的接口。
  • 实现接口为未实现接口的所有成员创建存根,因此您无需通过在代码窗格下拉列表中一一选择来手动创建它们:

    Implements IClass1
    
    Public Sub IClass1_DoSomething()
        Err.Raise 5 'TODO implement interface member
    End Sub
    
    Public Function IClass1_GetFoo() As Integer
        Err.Raise 5 'TODO implement interface member
    End Function
    
    Sub DoSomething()
    
    End Sub
    
    Function GetFoo() As Integer
    
    End Function
    


More refactoring tools are on the project's roadmap (including Extract Method), which you can follow on GitHub.

更多重构工具在项目的路线图上(包括Extract Method),你可以在GitHub 上关注

回答by user1701047

The only 'refactoring' tool I know of in VBA is Ctrl+F and Ctrl+R.

我在 VBA 中知道的唯一“重构”工具是 Ctrl+F 和 Ctrl+R。

回答by KFleschner

I use V-Tools for refactoring-like work as it will do find / replace in objects, not just VBA code. http://www.skrol29.com/us/vtools.php

我使用 V-Tools 进行类似重构的工作,因为它会在对象中查找/替换,而不仅仅是 VBA 代码。 http://www.skrol29.com/us/vtools.php

回答by Archlight

Yes there is.... almost

是的,有......几乎

In the good old days i used this one. http://www.moshannon.com/speedferret.html

在过去的美好时光里,我使用了这个。 http://www.moshannon.com/speedferret.html

helped me alot and I think i have the 3.5" disks somewere ;)

帮了我很多,我想我有一些 3.5" 磁盘;)

The trick is to copy your excel code to Access or VB6 and do your refactoring there. Replacing scope: solution is creative naming and using replace. spitting procedures... well thats a manual I'm sorry.

诀窍是将您的 excel 代码复制到 Access 或 VB6 并在那里进行重构。替换范围:解决方案是创造性的命名和使用替换。随地吐痰程序...好吧,那是手册,对不起。

It's usually not worth it unless you have some serious excel vba code, I would recoment converting most of it into c# or VB.Net dll where you can do refactoring, testing and some modern magic and only do as little you can in VBA.

通常不值得,除非您有一些严肃的 excel vba 代码,我建议将大部分代码转换为 c# 或 VB.Net dll,您可以在其中进行重构、测试和一些现代魔术,而在 VBA 中只能做尽可能少的事情。