Write to an already opened Excel file
Show older comments
The following MATLAB code works well for writing
eActiveSheetRange.Value
to the range
eActiveSheetRange
However, I have to keep the respective Excel file not opened by any application to make the writing process successful.
So, is there any way to keep the Excel file opened by, for example, Excel while making the MATLAB able to write to it without throwing errors and watch the new values be written?
excel = actxserver('Excel.Application');
wbooks = excel.Workbooks;
eWorkbook = wbooks.Open('C:\Users\Diaa\Desktop\test.xlsx');
eSheets = excel.ActiveWorkBook.Sheets;
sheet1 = eSheets.get('Item',1);
sheet1.Activate
eActiveSheetRange = get(excel.Activesheet,'Range','B1');
eActiveSheetRange.Value = 15;
eWorkbook.Save
excel.Quit
excel.delete
Edit 1
Following @J. Alex Lee's answer, I get this error

when running the following code while the file is opened by Excel at the same time:
FileName = 'C:\Users\Diaa\Desktop\test.xlsx';
SheetName = 'Sheet1';
SheetNum = find( sheetnames(FileName) == SheetName);
try
excel = actxserver('Excel.Application');
catch
excel = actxGetRunningServer('Excel.Application');
end
excel.Workbooks.Open(FileName).Sheets.Item(SheetNum).Activate
excel.Activesheet.Range('B1').Value = 88;
excel.Activeworkbook.Save
excel.Quit
excel.delete
Accepted Answer
More Answers (0)
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!