SAP loop a table and check next or previous values if it is same

I have an excel file that lists sales order numbers, line numbers, and qty.
There are sometimes same sales order numbers.
The robot first read the excel file, and then inputs the data to SAP.

SO Line qty
30xxxxxx 10 100
30xxxxxx 20 200
30aaaaaa 10 100
30bbbbbb 10 10

I need to combine the same sales order and process together.
When the robot loops through the table, it clicks “save” at the end of each iteration, and then goes to the next line.
But I want to avoid to click “save” if the next SO number is the same as the current SO number .
If the next SO number is the same as current one, proceed with the next line input.
If the next SO number is different as the current one, save the current data, and go back to the first step.

I tried to check if the current SO number equals with the next (or previous) one (image below)but it does not work because the first row and the last does not have the previous or next row.

How do I loop through this kind of process?


I think it’s better to use Dictionary which is grouped by SO

dict = dt.AsEnumerable.GroupBy(Function(r) r("SO").ToString).ToDictionary(Function(g) g.Key,Function(g) g.CopyToDataTable)

dict is Dictionary<string,DataTable> type.


Then we can iterate SO in outer ForEach and iterate rows which has same SO in inner ForEachRow.


Hi @Yoichi
Thank you so much for your answer and apologize for the late response.

I tried the code, and have a question. The code groups by SO, and I understand that. Does this dictionary have other row values such as qty linked to the SO? Does the dictionary have one column only for SO?

I am asking this because I can get the SO as Key correctly, but the Value is empty.

Should the dictionary have the values linked to SO as Value?


The datatable in the dictionary has each rows of the original datatable as it is. Can you check it as the following, for example?


Here’s what I did.

This is the result.

What is wrong with my code??

The table has the same SOs but different qty. (meaning multiple line items)


It seems normal because when we display Dictionary<String,DataTable> instance in locals panel, we cannot see content of the datatable,.

If we check datatable directly in ForEach loop, for example, we can see content of the datatable, as the following.

Can you also check the following sample? (2.9 KB)


I understood and it worked.

Thank you so much for your help.

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