MATLAB Answers

Add a new Excel sheet from Matlab

220 views (last 30 days)
Derek Jing
Derek Jing on 6 Mar 2011
Commented: pravesh verma on 7 Jul 2016
Hi,
I am using ActiveX server to control Excel spreadsheet. Now I am using Worksheet.Sheets.Add to add a new sheet, but it add before the currently active sheet. I do not know how to apply the 'After' option there.
In all, I want to add a new sheet to the end (the last sheet), and set a new for this new sheet. Anyone can tell me how to apply 'After' and 'name' options to the Matlab command?
Thanks in advance for your help.
Regards, Derek
  1 Comment
pravesh verma
pravesh verma on 7 Jul 2016
sir plz provide all matlab sheet

Sign in to comment.

Accepted Answer

jiro
jiro on 6 Mar 2011
According to the VBA documentation on MSDN, to add after the active sheet, specify the active sheet as the second argument to the Add method:
Worksheet.Sheets.Add([], ActiveSheet)
Here's an example that adds a new sheet after the last worksheet (example assumes an Excel file called "Book1.xlsx" in the current folder):
% Connect to Excel
Excel = actxserver('excel.application');
% Get Workbook object
WB = Excel.Workbooks.Open(fullfile(pwd, 'Book1.xlsx'), 0, false);
% Get Worksheets object
WS = WB.Worksheets;
% Add after the last sheet
WS.Add([], WS.Item(WS.Count));
% Save
WB.Save();
% Quit Excel
Excel.Quit();
  4 Comments
Image Analyst
Image Analyst on 21 Oct 2014
Ilays, it IS simple. There are only 5 lines of code there. The rest is just comments to explain it.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!