使用 Sublime Text 或 Notepad++ 作为 VBA 的编辑器和生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32991011/
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
Using Sublime Text or Notepad++ as Editor and Builder for VBA
提问by Holene
OK, so I have to learn VBA in my new line of work as we use Excel to more or less anything. The built-in VBA editor in Excel drives me absolutely bonkers - it feels like it's not made for writing code in an efficient way.
好的,所以我必须在我的新工作中学习 VBA,因为我们或多或少地使用 Excel。Excel 中的内置 VBA 编辑器绝对让我发疯 - 感觉它不是为以有效的方式编写代码而设计的。
I have been using Sublime Text for all coding purposes recent years. There's a plug-in for writing VBAvbScriptin Sublime, but I don't understand how to link my Sublime written code to my Excel documents. Say I try to run the following file
近年来,我一直将 Sublime Text 用于所有编码目的。有一个用于在 Sublime 中编写VBA vbScript的插件,但我不明白如何将我的 Sublime 编写的代码链接到我的 Excel 文档。假设我尝试运行以下文件
Sub test()
Debug.Print "Hello, World"
End Sub
from Sublime Text, I get the following out from the console [Finished in 0.1s], but no printout.
从 Sublime Text,我从控制台得到以下内容[Finished in 0.1s],但没有打印输出。
My question is this: does anyone have a solution for how to run VBA in Excel from Sublime Text or Notepad++? I'm still a massive noob in VBA, therefore it would be nice to be able to actually see the effects on the Excel Workbook next to testing the code.
我的问题是:有没有人有关于如何从 Sublime Text 或 Notepad++ 在 Excel 中运行 VBA 的解决方案?我仍然是 VBA 中的一个大菜鸟,因此能够在测试代码旁边实际看到对 Excel 工作簿的影响会很好。
回答by Gaudi
The problem with VBA code and writing it in Sublime is that VBA is embedded into Excel, so you cannot just build the macros you write in Sublime
VBA 代码并用 Sublime 编写的问题在于 VBA 嵌入到 Excel 中,因此您不能只构建在 Sublime 中编写的宏
- One solution to this is writing your VBA code and copying-pasting into VBA Editor (mentioned above). This is not manageable when you start working with multiple modules, classes, etc.
- 对此的一种解决方案是编写 VBA 代码并将其复制粘贴到 VBA 编辑器中(如上所述)。当您开始使用多个模块、类等时,这是无法管理的。
- Second approach is to use Import/Export functionality of VBA. You can export your modules and classes into
*.basand*.clsfiles and edit them directly in Sublime Text. For that you can use macros from Ron De Bruinwebsite. Just copy the code into seperate module and it will export all code you have in the currect project into seperate files, which you can edit in Sublime Text. After you make changes in Sublime just import all files back again into VBA editor with the macro. You might want to change the VBA code slightly as well by commenting out
- 第二种方法是使用 VBA 的导入/导出功能。您可以将模块和类导出到
*.bas和*.cls文件中,并直接在 Sublime Text 中编辑它们。为此,您可以使用Ron De Bruin网站上的宏。只需将代码复制到单独的模块中,它就会将您在当前项目中拥有的所有代码导出到单独的文件中,您可以在 Sublime Text 中对其进行编辑。在 Sublime 中进行更改后,只需使用宏将所有文件重新导入 VBA 编辑器。您可能还想通过注释掉稍微更改 VBA 代码
If wkbSource.VBProject.Protection = 1 Then
MsgBox "The VBA in this workbook is protected," & _
"not possible to export the code"
Exit Sub
End If
... otherwise it won't import back to the same spreadsheet.
...否则它不会导入回同一个电子表格。

