vba 通过 VBS 打开 Excel 并在那里运行宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40652713/
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
Open Excel by VBS and run a macro there
提问by Michael Shtefanitsa
I need run a VBA macro in Excel but from VBS file. My code for opening Excel file and writing some values is below:
我需要在 Excel 中但从 VBS 文件中运行 VBA 宏。我打开 Excel 文件并写入一些值的代码如下:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\user\Desktop\test1.xlsm")
objExcel.Application.Visible = True
objExcel.Cells(1,2).Value = "Test value"
WScript.Quit
I have 2 simple macros in this file: test1
and test2
. I need run them by VBS.
我在这个文件中有 2 个简单的宏:test1
和test2
. 我需要通过 VBS 运行它们。
采纳答案by Michael Shtefanitsa
I need to put somethink like:
我需要提出一些想法:
objExcel.Application.Run "test1"
after
后
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\user\Desktop\test1.xlsm")
an my code will look like:
我的代码将如下所示:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\user\Desktop\test1.xlsm")
objExcel.Application.Visible = True
objExcel.Application.Run "test1"
objExcel.Application.Run "test2"
回答by Martin Router King
To run macros from other macros you only have to add these lines to your code... As long as you have saved you macros in the personal sheet the code should look like this... If you have saved them in another sheet, then change the part of the code where it says personal.xlsb for your sheet's name...
要从其他宏运行宏,您只需将这些行添加到您的代码中...只要您将宏保存在个人工作表中,代码应该如下所示......如果您将它们保存在另一个工作表中,那么更改代码的部分,它为您的工作表名称显示personal.xlsb...
Application.Run "PERSONAL.XLSB!test1"
Application.Run "PERSONAL.XLSB!test2"