vba 在 XLSX 文件上运行 Excel 宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45039149/
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
Run Excel Macro On XLSX File
提问by David
Our business system saves a report in an XLSX file format, which is standard Excel. I have a macro that I have written that processes this report to give me usable data. I had to write it in an XLSM file, which is a macro enabled excel spreadsheet. Is there a way I can run the macro on the original file without having to copy and paste the code in? I have seen some VBS scripts that run macros that already embedded into the spreadsheet, but this is slightly different.
我们的业务系统以 XLSX 文件格式保存报告,这是标准的 Excel。我编写了一个宏来处理此报告以提供可用数据。我不得不将它写在一个 XLSM 文件中,这是一个启用宏的 Excel 电子表格。有没有一种方法可以在原始文件上运行宏而无需复制和粘贴代码?我见过一些运行已嵌入电子表格的宏的 VBS 脚本,但这略有不同。
回答by Darren Bartrup-Cook
Just set a reference to the XLSX file:
只需设置对 XLSX 文件的引用:
Sub Test()
Dim OtherWorkbook As Workbook
'Otherworkbook should be closed at start of code.
'We open it here.
Set OtherWorkbook = Workbooks.Open("full path to other workbook")
With OtherWorkbook
.Worksheets("Sheet1").Range("A1") = "I've just updated the other workbook."
.Save
.Close
End With
End Sub
回答by Rafa Gomez
You could just do like this
你可以这样做
Dim reportWb = Workbooks.Open('workBookPath')
Dim reportSheet = reportWb.Sheets(SheetNr or SheetName)
Then you could do your macro
然后你可以做你的宏
回答by braX
Instead of using an XLSM file, use an XLAM file (Excel Add-In format) and then add it to Excel as an Add-In. Then the code can be run on any spreadsheet loaded into memory at the time.
不要使用 XLSM 文件,而是使用 XLAM 文件(Excel 加载项格式),然后将其作为加载项添加到 Excel。然后可以在当时加载到内存中的任何电子表格上运行代码。