MATLAB: Excel Range in specifies Sheet.

excel range cell sheet

Hi, with the following code in the file xxxx I write a name in the A3 range. This name is saved in the Book Scope (1) and not in the Sheet (2).
ex = actxserver('excel.application');
ex.Workbooks.Open(fullfile(pwd,'XXXXXXX.xlsx'));
ex.Range('A3').Name = 'RangeName';
ex.ActiveWorkbook.Save
ex.Quit
ex.release
I can write the name on a specifies sheet??
Thanks

Best Answer

Hi, Just activating the particular sheet before adding range will solve it.
ex = actxserver('excel.application');
ex.Workbooks.Open(fullfile(pwd,'XXXXXXX.xlsx'));
Sheets = Excel.ActiveWorkBook.Sheets;
Sheet3 = get(Sheets, 'Item', 'Sheet3');
Sheet3.Activate
ex.Range('A3').Name = 'RangeName';
ex.ActiveWorkbook.Save
ex.Quit
ex.release