For Each: Replace value in a datatable column


I have a datatable filled with data.
In a for each loop I want to replace a value. How do I do this without exporting it to excel and using write cell activity?

Currently my loop looks like this. I wrote the datatable to csv after, but it doesn’t show the new values. I don’t get any error messages.

Assign activity is: row(4).ToString.padleft(4,CChar(“0”))


1 Like


I think the issue is not with the UiPath processing, but in the type of the Excel column.

You can change the column type of the excel to “Text” and it works as expected as you can see below.


Happy coding! :slight_smile:

Hi Udhay,

The original datatable is a csv file, changing the value in excel to text doesn’t do anything for csv.

Is there column with index 4 in your datatable? Try Dt.Columns.Count to see if it exists. You can always add a column with a specific name for index 4 before going into ForEachRow.

I always find it more reliable to work with column names instead of indices (in case column order changes).
instead of row(4) I would use


Yes, there is always a column index 4.
I understand what you mean for clarity purposes, but I don’t think this answers my question(?)

It is a legitimate question, because if your if statement never gets to the “true” side, then your assign activity will never trigger. Don’t forget that index starts at 0, so row(4) is actually the 5th column in the datatable.

I’m not 100% sure if it’s necessary, but try changing the right side of your assign activity to be row(4).Item.ToString.padleft(4,CChar("0")) instead,

Also, throw in a write line activity or something similar to show which path you are taking in your if statement. This can greatly help debugging your logic and is something i generally recommend doing all the time during development as it can be removed when tidying things up for the actual deployment

1 Like

Yes, I undestand and thank you all for trying to help me make my project more readable.
I use write line a ton, which is something that is included in the commented out section.
Other than that, please let’s just stay on topic of what I’m trying to achieve :slight_smile:


This returns a compiler error: Option Strict On disallows late binding.

We are attempting to help you with the limited information you have given us. We gave the likely problem based on that limited information, and offered some direction on how to try to troubleshoot it to give us some more information. It seems like you know what you’re doing though, so good luck

1 Like

Agree with Dave, it is not clear whether your If statement goes to True.

You could also try adding .ToString to your If condition:
convert.Toint32(row("Class").ToString) < 4

1 Like


Have a look to the below thread :-

In that post also it is not solved yet.

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer:

1 Like

I remade the activities and wrote your code in and it seemed to work. Thank you

@Pratik_Wavhal, that’s also a helpful topic, thanks for linking this!

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