Hi,I’m trying to add 2 columns(Num1 and Num2) and assign the sum in 3rd col (Sum) in excel. I’ve done followings:-
ExcelApp Scope -Read Range with output datatable (datatable as DtAdd).
For each data loop(declaring 3 variables(vNum1,vNum2,vSum) and performed addition).
Add data row (with Arrayrow as {vNum1,vNum2,vSum} and datatable as DtAdd).
4.Putting it into output datatable and trying to print the string.
My problem is i’m able to see and set all variables including Sum inside loop(while disabling Add data row).But, when using the whole flow with Add data row, it is throwing an error as
For each row: Collection was modified,enumeration operation might not execute.
Can you pls help and explain me why this is happening?
Why are you using add data row, its not required ,you have columns defined in your excel already, so you don’t have to use add data row again, refer the workflow.
I’m able to solve the problem(using your help…thanks) , but just wondering why error was occurring for add data row?So,in which case we should you use add data row?..sorry , i’m learning uipath and saw in some earlier videos usage of add data row to fill up data into the DT, so was trying out.
Suppose you have 3 rows in your excel, but you want to add a new row.
Then you can use add data row and specify the values {“10”,“11”} and store in datatable, now your datatable has 4 rows and you can perform the actions on all these 4 rows and use write range to print the values.
As the error says, you cannot modify the collection you are iterating over inside foreach. This applies primarily to changing the length of it (so adding and deleting).
Short version of why - this is to prevent hard to trace errors like indirect infinite loops (if you always add a row, it would never end as there’s always a next element) or iteration ambiguity (if I delete current element and row indexes will change, what should be the next element?). Also there can be no collection at all.