How to remove rows that have completed iteration from a DataTable help

Hello,
I would like to inquire about a method to remove rows from a DataTable in UiPath where the repetition is completed. I want to remove the rows in a DataTable within a “Foreach row” activity. As a UiPath beginner, I appreciate your assistance!
Thank you in advance for your response.

Hi @22222222asas

You can use an if condition and remove data row activity in it


For Each Row in dt

    If condition_to_remove_row_met
        Remove Data Row: Input = Row
        Break

End the For Each Row activity

Hope it helps!!

1 Like

@pravallikapaluri
Thank you for your response!
Could you possibly explain with images or code?
I’m still a beginner and haven’t fully grasped the understanding.
I apologize for any inconvenience

Hi @22222222asas

Inside for each row in datatable activity if you place the remove data row activity it will not work, For each row in datatable will not allow the Remove Data row activity to remove the rows.

Instead of using the remove data row activity, use the LinQ Expression to delete the rows in the datatable.

Provide the condition to which row has to delete, then we will give you an expression to delete the rows in the datatable by simply using assign activity.

Hope it helps!!

1 Like

Hi @22222222asas

Use the invoke vba and place the below code within that and place the condition in that and also change the column index number as per your requirement.

  Dim lastRow As Long
    Dim i As Long

    ' Set the worksheet where you want to perform the operation
    Set ws = ThisWorkbook.Sheets(PerformanceSheet) ' Replace "Sheet1" with the actual sheet name

    ' Find the last row with data in column A (EMP ID column)
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    ' Loop through each row from bottom to top
    For i = lastRow To 2 Step -1
   
        If ws.Cells(i, 1).Value = "Give your condition here" Then ' Assuming EMP ID is in column A (change the column number if different)
            ' Delete the entire row if EMP ID is empty
            ws.Rows(i).Delete
        End If
    Next i
End Sub
1 Like

@22222222asas

As the Data in the datatable is going to change after Removing data from datatable.It will throw an error that “Collection is modified” using for each

Use the linq Query instead of it after reading the excel

dtInput = dtInput.AsEnumerable().GroupBy(Function(row) row.Field(Of String)("ColumnName")).Select(Function(group) group.First()).CopyToDataTable()

Hope it helps!!

1 Like

@mkankatala
Thank you for your response!
I would like to know how to delete rows after each iteration, so that the rows processed in each iteration are removed, allowing the next row to be processed.
Could you please provide guidance on this?

@vrdabberu
Thank you for your response!
Could you possibly explain with images or code?
I’m still a beginner and haven’t fully grasped the understanding.
I apologize for any inconvenience

There is no need to delete the processed row because if we loop each row in datatable using for each row in datatable activity, it will iterate through each row, if the row is processed it will proceed to the future rows. It will not touch the processed rows in datatable.

Check the below image for better understanding,

Hope you understand!! @22222222asas

1 Like

@mkankatala
Thank you for the response.
However, I am using ‘foreach row in datatable’ inside another ‘foreach row in datatable,’ and I want to delete rows that have been processed in each iteration. Is there a way to achieve this?

No there is no possibility to delete the rows in the for each row in datatable activity. @22222222asas

It will throw the below error, if we attempt to delete the row.

@mkankatala

After running 2 ‘foreach row in datatable’ loops, then returning to 1 ‘foreach row in datatable,’ and again to 2 ‘foreach row in datatable,’ the rows reset to 1. I want to remove the rows that have been processed to prevent this. Is there a way to achieve this?

@pravallikapaluri
Thank you for your response!
After running 2 ‘foreach row in datatable’ loops, then returning to 1 ‘foreach row in datatable,’ and again to 2 ‘foreach row in datatable,’ the rows reset to 1. I want to remove the rows that have been processed to prevent this. Is there a way to achieve this?

Hi @22222222asas ,
What do you want to use them for?
I feel this needs clarification
Because a loop goes through each line in turn, if you delete them, the original data will change, and the position will also change, possibly the next action will be wrong.
Regards,