简体   繁体   English

通过VBS打开Excel并在其中运行宏

[英]Open Excel by VBS and run a macro there

I need run a VBA macro in Excel but from VBS file. 我需要在Excel中但从VBS文件运行VBA宏。 My code for opening Excel file and writing some values is below: 我打开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 . 我在此文件中有2个简单的宏: test1test2 I need run them by VBS. 我需要由VBS运行它们。

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"

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM