Using an Excel Macro from the PERSONAL.XLSB Workbook

3 次查看(过去 30 天)
Hi,
I have a script that generates an excel file for me and then a Macro that does all of the formatting of the file withing excel. I saved the Macro in My Personal Macro Workbook, and I would like to be able to call it from there and apply it to the excel file that I generate. We will call that file "WorkbookName.xlsx". I've seen that it is definitely possible to have MATLAB run a macro in excel on a specific workbook, but I can seem to figure out how to get it to pass my macro from the Personal Macro Workbook to my current workbook.
file_location = '\\myfilelocation\WorkbookName.xlsx';
ExcelApp = actxserver('Excel.Application');
% Open file
Workbook = ExcelApp.Workbooks.Open(file_location);
% Show window (optional).
ExcelApp.Visible = 1;
% Run WAD_Formatting macro from Personal Macro Workbook,
ExcelApp.Run('PERSONAL.XLSB!.WAD_Formatting');
% Quit application and release object.
ExcelApp.Quit;
ExcelApp.release;

回答(1 个)

jr1995
jr1995 2023-1-27
移动:Cris LaPierre 2023-3-2
Hi,
the following code worked for me. Beside the file I would work on, I also opened my personal workbook. Then I could execute the embedded macros.
%testing with excel
ExcelApp = actxserver('Excel.Application');
ExcelApp.Workbooks.Open("path_to_personal_macro_workbook\PERSONAL.XLSB"); %Import personal macros
excelWb=ExcelApp.Workbooks.Open("path_to_excel_table.xlsx"); % Open up the workbook named in the variable fullFileName.
ExcelApp.Visible = true; %Make workbook visible
ExcelApp.Run('PERSONAL.XLSB!MethodName'); %Run arrhenius macro

类别

Help CenterFile Exchange 中查找有关 Debugging and Analysis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by