フィルターのクリア

Loop for end before it should

1 回表示 (過去 30 日間)
Ali
Ali 2022 年 10 月 5 日
コメント済み: VBBV 2022 年 10 月 5 日
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.

採用された回答

VBBV
VBBV 2022 年 10 月 5 日
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 件のコメント
Ali
Ali 2022 年 10 月 5 日
@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
VBBV
VBBV 2022 年 10 月 5 日
Ok. Then I think it's related to or something more to do with your other GUI application components.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

製品


リリース

R2012a

Community Treasure Hunt

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

Start Hunting!

Translated by