我正在尝试从 Excel 文件外部运行 Excel 宏。我目前正在使用从命令行运行的“.vbs”文件,但它一直告诉我找不到宏。这是我尝试使用的脚本
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("test.xls")
objExcel.Application.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(1, 1).Value = "Test value"
objExcel.Application.Run "Macro.TestMacro()"
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Echo "Finished."
WScript.Quit
这是我试图访问的宏:
Sub TestMacro()
'first set a string which contains the path to the file you want to create.
'this example creates one and stores it in the root directory
MyFile = "C:\Users\username\Desktop\" & "TestResult.txt"
'set and open file for output
fnum = FreeFile()
Open MyFile For Output As fnum
'write project info and then a blank line. Note the comma is required
Write #fnum, "I wrote this"
Write #fnum,
'use Print when you want the string without quotation marks
Print #fnum, "I printed this"
Close #fnum
End Sub
我尝试了位于是否可以通过外部命令在 Excel 中运行宏? https://stackoverflow.com/questions/3183286/is-it-possible-to-run-a-macro-in-excel-from-external-command到目前为止(当然也进行了修改),但它似乎不起作用。我不断收到错误“Microsoft Office Excel:找不到宏“Macro.TestMacro”。
编辑:Excel 2003。
好吧,其实很简单。假设您的宏位于模块中,而不是在其中一张表中,您可以使用:
objExcel.Application.Run "test.xls!dog"
'notice the format of 'workbook name'!macro
对于包含空格的文件名,请用引号将文件名引起来。
如果您已将宏放置在工作表下,例如工作表 1,则只需假设工作表 1 拥有该函数,事实也是如此。
objExcel.Application.Run "'test 2.xls'!sheet1.dog"
注意:您不需要一直使用的 Macro.testfunction 符号。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)