Search code examples
excelmatlabactivex

Matlab - ActiveX Move Excel Sheet


Which is the command to move an excel sheet after another from one book to other?. Commented lines failed.

X = actxserver('Excel.Application'); 
XW0 = X.Workbooks.Open('t0.xlsx'); 
XW2 = X.Workbooks.Open('t2.xlsx'); 
XW2.Worksheets.Item(1).Name = 't2';
% XW2.Worksheets.Item(1).Move('after',XW0.Sheets(1));
XW0.Save
XW0.Close(false)
XW2.Save
XW2.Close(false)
X.Quit

Solution

  • I was able to get Sheet t2 to move from t2.xlsx to after Sheet 1 in t0.xlsx by replacing your commented-out line with this:

    XW2.Worksheets.Item(1).Move([], XW0.WorkSheets.Item(1));
    

    Note the empty matrix for the first argument, which is where the Before option goes (see here).