How can I run different Excel Macros buttons from MATLAB?

조회 수: 9 (최근 30일)
AJD
AJD 2018년 7월 2일
답변: DUY Nguyen 2023년 3월 2일
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
Saurabh Singhal
Saurabh Singhal 2023년 3월 1일
Hi. Were you able to figure this out? I am trying to achieve something similar but don't know how to.
Thanks.

댓글을 달려면 로그인하십시오.

답변 (1개)

DUY Nguyen
DUY Nguyen 2023년 3월 2일
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;

카테고리

Help CenterFile Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by