Filter löschen
Filter löschen

Excel Range in specifies Sheet.

1 Ansicht (letzte 30 Tage)
Aitor Vitoria
Aitor Vitoria am 17 Mai 2016
Kommentiert: Rangan M K am 19 Mai 2016
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

Akzeptierte Antwort

Rangan M K
Rangan M K am 17 Mai 2016
Bearbeitet: Rangan M K am 17 Mai 2016
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
  2 Kommentare
Aitor Vitoria
Aitor Vitoria am 17 Mai 2016
Hi Rangan, Thanks but with this code you are writing in the book and I want Write the name in the Sheet3.
Thanks
Rangan M K
Rangan M K am 19 Mai 2016
Hi, Aitor Vitoria It does write in Sheet3. ex = actxserver('excel.application');
ex.Workbooks.Open(fullfile(pwd,'XXXXXXX.xlsx'));-> Open file and get handle
Sheets = Excel.ActiveWorkBook.Sheets; -> Gets hadle of the Sheets
Sheet3 = get(Sheets, 'Item', 'Sheet3');->Get handle of Sheet3
Sheet3.Activate ->Activate sheet3
ex.Range('A3').Name = 'RangeName'; ->Give name to the range
ex.ActiveWorkbook.Save ->Save changes
ex.Quit
ex.release
Hope this is what you wanted

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Import from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by