是否可以从外部命令在Excel中运行宏? [英] Is it possible to run a macro in Excel from external command?
问题描述
您可以启动Excel,打开工作簿,然后操作工作簿一个VBScript文件。
将下面的代码复制到记事本。
更新MyWorkbook.xls和 Sheet1'参数。
使用vbs扩展名保存并运行。
Option Explicit
错误恢复下一步
ExcelMacroExample
子ExcelMacroExample()
Dim xlApp
Dim xlBook
设置xlApp = CreateObject(Excel.Application)
设置xlBook = xlApp.Workbooks.Open(C:\MyWorkbook.xls)
xlBook.Sheets(Sheet1)。Cells(1,1).Value =My text
xlBook.Sheets(Sheet1)。Cells(1,1).Font.Bold = TRUE
xlBook.Sheets(Sheet1)。单元格(1,1).Interior.ColorIndex = 6
xlBook.Save
xlBook.Close
xlApp.Quit
设置xlBook =没有
设置xlApp =没有
End Sub
以上代码启动Excel打开工作簿,输入单元格A1中的值使其变为粗体并更改单元格的颜色。然后保存并关闭工作簿。 Excel然后关闭。
Let's say I want to program a VBA code in an external program that opens an Excel file, runs a macro, saves (and say yes to any pop up windows), and close Excel. Is it possible to do so? If so, how would I go about implementing it?
You can launch Excel, open a workbook and then manipulate the workbook from a VBScript file.
Copy the code below into Notepad.
Update the 'MyWorkbook.xls' and 'Sheet1' parameters.
Save it with a vbs extension and run it.
Option Explicit
On Error Resume Next
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\MyWorkbook.xls")
xlBook.Sheets("Sheet1").Cells(1, 1).Value = "My text"
xlBook.Sheets("Sheet1").Cells(1, 1).Font.Bold = TRUE
xlBook.Sheets("Sheet1").Cells(1, 1).Interior.ColorIndex = 6
xlBook.Save
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
This code above launches Excel opens a workbook, enters a value in cell A1, makes it bold and changes the colour of the cell. The workbook is then saved and closed. Excel is then closed.
这篇关于是否可以从外部命令在Excel中运行宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!