MATLAB Answers

How can I rename a sheet in Excel using the COM interface in MATLAB?

96 views (last 30 days)
I am using ACTXSERVER to control Excel. I have the following code to open a document, and I would like to rename an existing sheet in the workbook.
filename = 'C:\SomeExcelFile.xls';
% Open Excel Automation server
Excel = actxserver('Excel.Application');
% Make Excel visible
Excel.Visible=1;
% Open Excel file
Workbook = Excel.Workbooks.Open(filename);
% Get the list of sheets in the workbook
Sheets = Excel.ActiveWorkbook.Sheets;

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Dec 2011
You can rename an Excel sheet using the "Name" property of a sheet object. For example the following renames the first sheet of an Excel file:
filename = 'C:\SomeExcelFile.xls';
% Open Excel Automation server
Excel = actxserver('Excel.Application');
% Make Excel visible
Excel.Visible=1;
% Open Excel file
Workbook = Excel.Workbooks.Open(filename);
% Get the list of sheets in the workbook
Sheets = Excel.ActiveWorkbook.Sheets;
% Rename the first sheet
Sheets.Item(1).Name = 'This is sheet 1';
The following code then saves the Workbook, quits Excel and removes the COM server:
% Save the file
Workbook.Save();
% Quit Excel, remove the COM server and delete the related objects
Excel.Quit();
Excel.delete();
clear Excel;
clear Workbook;
clear Sheets;

More Answers (1)

Nicolae Pop
Nicolae Pop on 4 Dec 2020
I have a sheet in Excel and i want to export in Matlab 2020r, and then i save data in nume.mat, but when a script who use this data (load nume.mat ) appear the error o load?

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!