How to increment Row by 1 inside a for each Row

I have an excel sheet with column name “Product” and values as Apple,orange etc(values provided as dummy data.) as attached.
This has been read and stored as a Data Table,which is then filtered row wise inside a for each loop.I want to put in a condition where the current row value when not equal to the next row exit the loop.Please advise.Capture

Hi @amr07,

ForEach loop does not require a counter value to be incremented. It iterates through each of the records.

Use Distinct() method to get the unique values from the column.

dt_Sample.Select( Function(y) y.Item(“Product”).ToString).Distinct()

Hope this helps!


1 Like

You would use an if condition to check for the Row and its previous Row

Assign last As DataRow = Nothing
For Each row in Datatable
    If last IsNot Nothing Then
        IF row.item("Product").Tostring.equals(last.item("Product").Tostring)) Then
       End If
    End If
    last = row
1 Like

Its not about getting the distinct value,i wish to get the next row value while the for each iteration…something like previous values is “B” and the current value is “B” and If the nextvalue<>current value exit loop something like that.Hope it make sense

Let me try this out.Thank you for the reply.

1 Like

Well in that case the above solution provided by @TimK seems good.

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