How can i move excel sheets in the same file
Show older comments
Hello everyone!
I have an Excel file that contains many sheets.
the names of the sheets are something like this:
1_1,1 1_1,2 1_1,3 2_1,3 2_1,2 2_1,1 3_1,1 3_1,2 3_1,3 4_1,3 4_1,2 4_1,1 ....100_1,3 100_1,2 100_1,1
Is there any way to rearrange the sheets so they are in asceding order?
1_1,1 1_1,2 1_1,3 2_1,1 2_1,2 2_1,3 ...... 100_1,1 100_1,2 100_1,3
I have read this answer by Image Analyst https://www.mathworks.com/matlabcentral/answers/84997-moving-excel-sheets-within-excel-file
but if i try to copy all this sheets one by one, create new one ,paste it and then delete it and then move to the next, this would take a long time i believe.
Is there any other solution?
Thank you very much!
P.S: I have the option to rename the sheets like this 1 2 3 6 5 4 7 8 9 12 11 10 .... and then of course i would like them to rearranged as this 1 2 3 4 5 6 7 8 9 10 11 12...
4 Comments
Bob Thompson
on 11 Sep 2019
I don't know that you will be able to just rearrange the excel sheets using matlab. Most of matlab's interactions with excel are simply to read or write information, and it doesn't fully categorize the file's contents in an extensive enough manner to capture all of the sheet names. Commands like xlsread and xlswrite are certainly incapable of this.
You may have some luck with the actxserver ability, as this allows you to operate excel remotely with matlab, but I have not found any great resource for how to use the actxserver, and do not know of a way to organize the different sheets based on title alone with it.
Honestly, this sounds like something that would be much easier to do in excel directly, or using VBA code. Is there a reason you are trying not to do that?
alex
on 11 Sep 2019
Guillaume
on 11 Sep 2019
I have not found any great resource for how to use the actxserver
Matlab documentation on using COM objects is here and Excel's object model documentation is there. The code you'd write in matlab would be more or less identical to the one you'd write in VBA. The main difference is that matlab does not support default properties, so the VBA code:
' wb is an excel workbook
set ws = wb.Worksheets('SomeSheetName') 'Index the worksheets collection. Don't have to use Item as it's the default property
would translate in matlab to:
%wb is an excel workbook
ws = wb.Worksheets.Item('SomeSheetName'); %Item is the default property of the worksheets collection. Has to be called explicitly in matlab
If you're familiar with excel VBA, it's trivial to write the equivalent in matlab. If you're not, you've got a steep learning curve ahead of you.
alex
on 11 Sep 2019
Accepted Answer
More Answers (1)
Image Analyst
on 10 Sep 2020
For what it's worth, I have a static method in my Excel_utils class that lets you move a worksheet to be the first one. It's probably easy to modify it to be any index in the workbook:
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------
% Moves the worksheet named "sheetName" so that it is the very first worksheet in the workbook.
% Example call:
% Excel_utils.MoveToSheet1(Excel, 'Summary'); % Make 'Summary' worksheet be the first worksheet in the workbook.
function MoveToSheet1(Excel, sheetName)
try
firstSheet = Excel.Worksheets.Item(1); % Get object/handle of the first worksheet in the workbook.
currentSheet = Excel.Worksheets.Item(sheetName); % Get object/handle of the user-specified, named worksheet.
currentSheet.Move(firstSheet); % Move the specified worksheet to be before the first worksheet.
catch ME
errorMessage = sprintf('Error in function MoveToSheet1.\nThe Error Message:\n%s', ME.message);
fprintf(errorMessage);
end
return; % from MoveToSheet1
end % of the MoveToSheet1() method.
1 Comment
hxen
on 2 Jun 2023
awesome! was exactly what I was looking for a work around with writetable. very helpful. :)
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!