Clear Filters
Clear Filters

How can I run different Excel Macros buttons from MATLAB?

8 views (last 30 days)
I currently have a large excel file connected to a macros script. Within the excel file, there are 2 different buttons to run different variations of the same macros. Whenever I run the macros from MATLAB, it only runs the first button. My question is, is there a way that I can specify which button I want to push from MATLAB? For more background information, I am using an excel spreadsheet provided by the FAA, which can be found at: https://www.fire.tc.faa.gov/Systems/FuelTank/FTFAM. I would like to be able to differentiate between "Run Monte Carlo" and "Run Single Flight.
Thanks for the help
  1 Comment
Saurabh Singhal
Saurabh Singhal on 1 Mar 2023
Hi. Were you able to figure this out? I am trying to achieve something similar but don't know how to.
Thanks.

Sign in to comment.

Answers (1)

DUY Nguyen
DUY Nguyen on 2 Mar 2023
I found this with Chatgpt. You can try this:
"Yes, it is possible to specify which button to click in an Excel file using MATLAB. You can use the actxserver function in MATLAB to connect to Excel and then use the InvokeVerb method to activate the desired button. Here is some sample code that you can modify to fit your specific Excel file and macros:
% Start an ActiveX server for Excel
excel = actxserver('Excel.Application');
% Open your Excel file
workbook = excel.Workbooks.Open('C:\path\to\your\file.xlsx');
% Select the worksheet where your buttons are located
worksheet = workbook.Worksheets.Item('Sheet1');
% Get a reference to the "Run Monte Carlo" button
button1 = worksheet.Buttons.Item('Button 1');
% Get a reference to the "Run Single Flight" button
button2 = worksheet.Buttons.Item('Button 2');
% Invoke the "Run Monte Carlo" button
button1.InvokeVerb('LeftClick');
% Invoke the "Run Single Flight" button
button2.InvokeVerb('LeftClick');
% Close the workbook and quit Excel
workbook.Close;
excel.Quit;

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!