Skip to main content

Execute-Macro

Command Description

Execute macros in Excel

Command Prototype

objRet = Excel.ExecuteMacro(objExcelWorkBook,macro,listArgs)

Parameter Description

ParameterRequiredTypeDefaultDescription
objExcelWorkBookTrueexpressionobjExcelWorkBookExcel workbook object (workbook opened with "Open Excel" command (Excel.OpenExcel) or the workbook object returned by the command "Bind Excel" (Excel.BindBook))
macroTruestring""Macro definition in Excel. Can be sub or function
listArgsTrueexpression[]Need to pass defined attributes to Macro. If subSum(1,2) is called, then pass [1,2]

return

objRet,The variable used to save the output of the command.

Demo

/************************Execute macro************************** ******** 
**Input 1:
** objExcelWorkBook--Excel workbook object (a workbook opened using the "Open Excel" command (Excel.OpenExcel) or a job bound using the "Bind Excel" command (Excel.BindBook) book object).
**Entry 2:
** macro--Macro definition in Excel, which can be sub or function.
**Entry 3:
** listArgs--The parameters that need to be passed to the macro definition, such as calling subSum(1,2), pass [1,2].
**Exports:
** objRet--The variable to which the output of the function call is saved.
**
*********************************************************************/

objExcelWorkBook = Excel.OpenExcel(@res"Test.xlsx",True,"Excel","","")
objRet = Excel.ExecuteMacro(objExcelWorkBook,"test",[])
TracePrint "Execute macro: Excel object 'Sheet1' worksheet runs the macro to calculate the sum of the column where A1 is located"
Excel.CloseExcel(objExcelWorkBook,True)

/************************************************************************/

/*The macro code is as follows:
Sub test()
'
' test macro
'
'
Range("A1:A11").Select
Range("A11").Activate
ActiveCell.FormulaR1C1 = "=SUM(R[-10]C:R[-1]C)"
Range("A1:A11").Select
End Sub
*/