Macro in excel is getting removed while running MATLAB
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mohammad Azadi Tabar
el 17 de En. de 2024
Comentada: Mohammad Azadi Tabar
el 15 de Feb. de 2024
I am using MATLAB and Excel interface to run excel macro from MATLAB. I did not have any problem with this for a long time. However, recently (maybe due to some securrity or windows update), after few hours of runing the code, MATLAB fails to connect to excel and run the macro (it can happen after 2 hrs or 24 hr). Then when I check the macro in excel, I notice it has been deleted. There is no macro code available.
Also VBA macros are enabled.
The error is this:
Error using COM.Excel_Application/Run
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Cannot run the macro 'Sheet1.FirstExample'.
The macro may not be available in this workbook or all
macros may be disabled.
Help File: xlmain11.chm
Help Context ID: 0
3 comentarios
Walter Roberson
el 17 de En. de 2024
I wonder if it would be more robust if you were to use .NET instead of activex ?
Respuestas (1)
Aditya
el 24 de En. de 2024
Hi Mohammad,
I understand that you're facing issues with a VBA macro being deleted or not executing correctly when using MATLAB with Excel. To address this issue, you can follow these troubleshooting steps:
- Check Excel's Trust Center Settings: Ensure that Excel's Trust Center settings permit the execution of macros
- Test Macro in Excel Manually: Before running the macro from MATLAB, verify that it works correctly in Excel
Here's a sample VBA script you can use to test:
Sub FirstExample()
' This macro writes "Hello, World!" into cell G2 of the active sheet.
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
' Write the string "Hello, World!" to cell G2.
ws.Range("G2").Value = "Hello, World!"
' Inform the user that the macro has run successfully.
MsgBox "Macro 'FirstExample' has run successfully!", vbInformation, "Macro Complete"
End Sub
- Check for File Corruption: Save the workbook under a new name and see if the issue persists.
- Monitor File Access: Make sure the Excel file isn't being accessed by multiple processes concurrently
After confirming that macros are enabled in Excel and the macro runs correctly within Excel itself, use the following MATLAB code with a “try-catch-finally” block to handle errors gracefully and ensure resources are released properly:
try
% Start Excel application
e = actxserver('Excel.Application');
e.Visible = 1;
% Open the workbook
workbook = e.Workbooks.Open(fullfile(pwd, 'AspenRunCC.xlsm'));
% Run the macro from a module
e.Run('FirstExample');
% Alternatively, run the macro from a sheet
% e.Run('Sheet1.FirstExample');
% Save the workbook
workbook.Save();
% Quit Excel
e.Quit;
catch ME
disp('An error occurred:');
disp(ME.message);
finally
% Release the COM object
if exist('e', 'var')
e.release;
end
end
For further reference on running Excel macros from MATLAB, please refer to the following MATLAB Central Link:
Hope this helps!
Ver también
Categorías
Más información sobre MATLAB Functions in Microsoft Excel en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!