Loop for end before it should

hi,
I have in my function two for loops which are used to rename the sheets of an excel file. The first loop changes the name of the sheets that takes the values of a first list. The second loop changes the name of the sheets that takes the value of a second list. But sometimes the second loop end before changing all the names of the concerned sheets and sometimes it odes it perfectly i don't know why. Heres the code :
f = 1
g = 2
for i = 1:(length(nb)) %length(nb) represent the number of items selected in the first listbox
myExcel = actxserver('Excel.Application');
excelWorkBook = myExcel.Workbooks.Open('C:\Users\223080038\Desktop\data.xlsx',0,false);
excelWorkBook.Worksheets.Item(f).Name = [Varlist1{1,nb(1,i)} ' Spectre tension'];
excelWorkBook.Worksheets.Item(g).Name = [Varlist1{1,nb(1,i)} ' Impédence'];
excelWorkBook.Save;
excelWorkBook.Close;
myExcel.Quit;
f =f+2 %represent odd sheets
g=g+2 %represent even sheets
end
for e = 1:(length(ne)) %length(nE) represent the number of items selected in the first listbox
myExcel = actxserver('Excel.Application');
excelWorkBook = myExcel.Workbooks.Open('C:\Users\223080038\Desktop\data.xlsx',0,false);
excelWorkBook.Worksheets.Item(f).Name = [Varlist2{1,ne(1,e)} ' Spectre Courant'];
excelWorkBook.Worksheets.Item(g).Name = [Varlist2{1,ne(1,e)} ' Spectre tension'];
excelWorkBook.Save;
excelWorkBook.Close;
myExcel.Quit;
f=f+2
g=g+2
end
I don't understand why sometimes it work completly fine and others it doesn't finish it without any error codes. The first loop works perfectly fine and if there is only one loop weither the first or second it will also work fine.

 Accepted Answer

f = 1
g = 2
for i = 1:(length(nb)) %length(nb) represent the number of items selected in the first listbox
myExcel = actxserver('Excel.Application');
excelWorkBook = myExcel.Workbooks.Open('C:\Users\223080038\Desktop\data.xlsx',0,false);
excelWorkBook.Worksheets.Item(f).Name = [Varlist1{1,nb(1,i)} ' Spectre tension'];
excelWorkBook.Worksheets.Item(g).Name = [Varlist1{1,nb(1,i)} ' Impédence'];
excelWorkBook.Save;
excelWorkBook.Close;
myExcel.Quit;
f =f+2
g=g+2
end
f = 1
g = 2
for e = 1:(length(ne)) %length(nE) represent the number of items selected in the first listbox
myExcel = actxserver('Excel.Application');
excelWorkBook = myExcel.Workbooks.Open('C:\Users\223080038\Desktop\data.xlsx',0,false);
excelWorkBook.Worksheets.Item(f).Name = [Varlist2{1,ne(1,e)} ' Spectre Courant'];
excelWorkBook.Worksheets.Item(g).Name = [Varlist2{1,ne(1,e)} ' Spectre tension'];
excelWorkBook.Save;
excelWorkBook.Close;
myExcel.Quit;
f=f+2
g=g+2
end

7 Comments

VBBV
VBBV on 5 Oct 2022
Edited: VBBV on 5 Oct 2022
f = 1 % reset this values before start of 2nd for loop
g = 2 % reset this values
they aren't supposed to reset i forgot to inform about that. As f represent odd sheets and g represent pair sheets so i didn't reset them so that when we are done whith element from the first list they can be used for the next sheets
Ok. Let's say if nb is 7 and ne as 5 user selections from list box. In such case how will the code adapt to the user selections if f is not allowed to reset. Think of it as as trying to access the element that doesn't exist or which is empty.
Ali
Ali on 5 Oct 2022
Edited: Ali on 5 Oct 2022
In the excel file the 7 selection of nb will be on the 14 first sheets and the 5 selection of ne will take the sheets 15 to 24. (each item selected takes two sheet)
the code will proceed like this ;
for i = 1:(length(nb))
rename sheet 1 f=1
rename sheet 2 g=2
f = 3, g=4
rename sheet 3 f=3
rename sheet 4 f=4
f=5, g=6
...
at the end of the for f = 15 and g =16
end
then come the second loop for
for i = 1:(length(ne))
rename sheet 15 f=15
rename sheet 16 g=16
f=17, g=18
rename sheet 17 f=17
rename sheet 18 f=18
f=19, g=20
...
at the end of the second loop f=25 and g=26
and here the problem that can be seen is that sometimes it works great and other times without giving any error codes f and g will stop at 19 and 20 instead of 25 and 26 and it happens quite a lot.
%excelWorkBook.Close;
%myExcel.Quit
Ok. Then it's probably these two lines causing problem. Comment the above lines in first for loop. I guess you are referring to naming sheets acc to user selections in a single excel file.
@VBBV I didn't when i do that it will only ask me if i want to save over the old file and it stops the code from running even if click on yes
Ok. Then I think it's related to or something more to do with your other GUI application components.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2012a

Asked:

Ali
on 5 Oct 2022

Commented:

on 5 Oct 2022

Community Treasure Hunt

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

Start Hunting!