Error while adding 2 cols in Excel

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:-

  1. ExcelApp Scope -Read Range with output datatable (datatable as DtAdd).
  2. For each data loop(declaring 3 variables(vNum1,vNum2,vSum) and performed addition).
  3. 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?

-without add data row(able to retrieve Sum)

Hi @debosree,

Inside for each after the sum, use an assign activity and use

row(“Sum”)=VSum

and use write range and print the datatable.

1 Like

Refer the sample workflow and the input file

Sum.xlsx (7.9 KB)

Queue.xaml (10.3 KB)

1 Like

thanks… it worked.

Just curious to know, why is add data row throwing an error even after assigning row(“Sum”)=VSum(as mentioned by you)?

Hi,

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.

Hi,

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.

1 Like

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.

For longer and more precise information, google the exception or check Why can't change value in foreach?

1 Like

Thanks for your explanation.

@debosree
@andrzej.kniola

If you really need to delete from a collection while iterating over it you can loop through it backwards and the delete will be handled just fine.

for (int i = safePendingList.Count - 1; i >= 0; i--)
{
    // some code
    // safePendingList.RemoveAt(i);
}

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.