如何将 VBA 保存在宏中或作为宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20674464/
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
How to save VBA in a in or as Macro
提问by Behseini
I know How to Run a macro which is Recorded in Macro-enabled excel file but in case of Creating a Macro in Visual basic application - from a module I do not know how to save and run them for future use? Is there any way that I can save cosdes in visual basic As a MACRO? so user just run it from the list of existing Macros?
我知道如何运行记录在启用宏的 excel 文件中的宏,但是在 Visual basic 应用程序中创建宏的情况下 - 从模块我不知道如何保存和运行它们以备将来使用?有什么方法可以将 cosdes 保存在 Visual Basic 作为 MACRO 中吗?所以用户只需从现有宏列表中运行它?
Can you please let me know how to fix this?
你能告诉我如何解决这个问题吗?
回答by
Can you please let me know how to fix this?
Can you please let me know how to fix this?
Follow these steps
按着这些次序
Open your VBE ( Visual Basic Editor ) with ALT+F11(or Developer
tab then Visual Basic
)
使用ALT+ F11(或Developer
tab thenVisual Basic
)打开您的 VBE(Visual Basic 编辑器)
Right-click the VBA Project and Insert
a Module
(Standard coding module- not a class nor userform)
右键单击 VBA 项目和Insert
一个Module
(标准编码模块- 不是类或用户表单)
Copy-Paste the below code
复制粘贴以下代码
Sub YouCanSeeMe()
MsgBox "I am visible in the list of macros!"
End Sub
Private Sub YouCannotSeeMe()
MsgBox "I am not shown in the list because I am private" & vbCrLf & _
"You can only call me from VBA Code"
End Sub
Public Sub IAmPublic()
MsgBox "I am public so you can see me"
End Sub
Public Sub IAmPublicButYouCannotSeeMe(Optional bool As Boolean)
MsgBox "I am public but you can't see me in the list of macros " & vbCrLf & _
"because I need a parameter to run!"
End Sub
now, go back to the Developer
tab and click Macros ( or ALT+F8in the spreadsheet view )
现在,返回Developer
选项卡并单击宏(或电子表格视图中的ALT+ F8)
You should now see which subs are visible in the list and which are not.
您现在应该看到哪些子在列表中可见,哪些不可见。
As you can see it's all down to the access modifiers (Public/Private or default which is Public) and also the visibility in the macros list depends on the amount of parameters Sub requires.
如您所见,这完全取决于访问修饰符(公共/私有或默认为公共),并且宏列表中的可见性取决于 Sub 所需的参数数量。
Note there is also a Friend
keyword but it's not relevant in this case. You can google it if you want though.
请注意,还有一个Friend
关键字,但在这种情况下不相关。如果你愿意,你可以谷歌它。
now if you wanted to save some macros for future make sure that your Explorer is showing you all hidden files because AppData
is a hidden folder and if you don't see hidden files then you can't save your personal workbook in there.
现在,如果您想保存一些宏以备将来使用,请确保您的资源管理器向您显示所有隐藏文件,因为它AppData
是一个隐藏文件夹,如果您没有看到隐藏文件,则无法在其中保存您的个人工作簿。
Full description step-by-step is here
完整的分步说明在这里
but in easy words
但简单地说
you can save the workbook you just made with the above code in
您可以将刚刚使用上述代码制作的工作簿保存在
C:\Users\\AppData\Roaming\Microsoft\Excel\XLSTART
C:\Users\\AppData\Roaming\Microsoft\Excel\XLSTART
save is as Excel Binary Workbook
or .xlsb
extension.
保存为Excel Binary Workbook
或.xlsb
扩展名。
Now everytime you start Excel you have access to the macros that were saved in that workbook.
现在,每次启动 Excel 时,您都可以访问保存在该工作簿中的宏。
Note this only works on yourmachine.
请注意,这仅适用于您的机器。