One way to invoke an Excel macro from MATLAB is by launching Excel as a COM server from MATLAB using the 'actxserver' command.
Suppose there is an Excel file 'myFile.xls' containing the macro called 'Macro1'. The following commands should first open the Excel file and then run the macro:
ExcelApp = actxserver('Excel.Application');
ExcelApp.Visible = 1;
ExcelApp.Workbooks.Open(fullfile(pwd,'\myFile.xls'));
ExcelApp.Run('ThisWorkBook.Macro1', parameter1);
ExcelApp.Run('Sheet1.Macro2', parameter1, parameter2);
retVal = ExcelApp.Run('Macro3');
ExcelApp.Quit;
ExcelApp.release;
Please note the following:
- Macros defined in a sheet, ThisWorkBook or a module can be called with zero or more arguments depending on the method's signature.
- Only functions (not subs) defined in a module can return a value. An attempt to retrieve a value from a function defined in a sheet or ThisWorkBook will return "NaN" in MATLAB or "Empty" in VB/VBA.