Search code examples
matlabexcelcomvba

How can I invoke an Excel Macro in Excel from Matlab?


I would like to invoke an Excel Macro or an VBA function in Excel from Matlab. How should achieve this? I suppose I should create a handle with

filename = './sampleWorkbook';
Excel = actxserver('Excel.Application');
workbook = Excel.Workbooks.Open(filename);

Then how should I proceed to invoke an VBA funciton in that Excel workbook filename?


Solution

  • You are almost there. Make a call to Application.Run("macroname") and you should be fine. Make sure macroname is in a VBA Module in your Excel Workbook and has Public scope.

    filename = './sampleWorkbook';
    Excel = actxserver('Excel.Application');
    workbook = Excel.Workbooks.Open(filename);
    Excel.Application.Run("macroname");
    

    Here is a link to MSDN documentation for Application.Run : https://msdn.microsoft.com/en-us/library/office/ff197132.aspx