0
votes

I need to generate a code in Matlab to Edit and then save an Excel file. The code has to do the following:

  1. Open the Excel file

  2. Go to the second sheet (sheet2)

  3. Change the value of cell A2

  4. Go to the first sheet (sheet1)

  5. 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');

1
Wouldn't sheet2 == Activesheet there? And if you're making it visible, you might want to hard code a handle to the workbook instead of using Excel.ActiveWorkBook since this will fail once the user opens another workbook while you're not done.scenia

1 Answers

0
votes

For this kind of stuff, you can directly go to the Microsoft docs.

These should help you out:

http://msdn.microsoft.com/en-us/library/office/ff841185.aspx

http://msdn.microsoft.com/en-us/library/office/ff198017.aspx

Basically, you can issue the commands here, as you would in the SaveAs dialog in the Excel GUI.