I need to generate a code in Matlab to Edit and then save an Excel file. The code has to do the following:
Open the Excel file
Go to the second sheet (sheet2)
Change the value of cell A2
Go to the first sheet (sheet1)
Save the sheet1 as a tab delimited text file (.txt)
Sheet 1 contains cells which depend on the cell A2 in sheet2 (I need to do this several times for different values of sheet2-A2, that's why I need to code it).
Browsing the web, I have been able to code steps 1-4 (see code bellow). However, I have not been able to find the way to do 5. I would very much appreciate your help. Thank you!!
% Open Excel Server:
Excel = actxserver('Excel.Application');
% Makes Excel visible in the screen:
set(Excel, 'Visible', 1);
% Open Excel file:
Workbooks = Excel.Workbooks.Open('E:\TEST.xlsx');
% Make the second sheet active:
Sheets = Excel.ActiveWorkBook.Sheets;
sheet2 = get(Sheets, 'Item', 2);
invoke(sheet2, 'Activate');
% Get a handle in the active sheet:
Activesheet = Excel.Activesheet;
% Edit cell A2:
A = 2;
ActivesheetRange = get(Activesheet,'Range','A2');
set(ActivesheetRange, 'Value', A);
pause(5) %Pauses the code for 5 seconds (so sheet 1 updates the formulas)
% Going to Sheet1 where the focal data is:
Sheets = Excel.ActiveWorkBook.Sheets;
sheet1 = get(Sheets, 'Item', 1);
invoke(sheet1, 'Activate');
sheet2 == Activesheet
there? And if you're making it visible, you might want to hard code a handle to the workbook instead of usingExcel.ActiveWorkBook
since this will fail once the user opens another workbook while you're not done. – scenia