Update excell value

|CaseID|ServiceNu|Name|Status|Date

|3|120789|Joe Biden||
|5|120789|Joe Biden||
|9|256437|Donald Trump||
|12|256437|Donald Trump||
|25|256437|Donald Trump||
|10|352758|Chris Evans||
|35|352758|Chris Evans||
|45|352758|Chris Evans||

how to update Status and Date of row where ID equal to 12 and 25?

Hi @Ellen ,

Step 1 : Store the table in a data table variable.

Step 2 : Use a for each row in data table activity to iterate through the rows of the table.

Step 3 : Inside the For each row in datatable activity use a condition to check the Id , like row(“CaseID”).tostring =12 and in else part of the if condition use the next condition, row(“CaseId”).tostring =25

Step 4 : In the then part of each condition,
Use assign activity to update the column value

Row(“columnname”)= value to update with

Hi @Ellen

=> Read Range Workbook
image
Output → dt

=> Use the below code in Invoke Code:

Dim rowsToUpdate = From row In yourDataTableVariable.AsEnumerable()
                   Where row.Field(Of Double)("CaseID") = 12 OrElse row.Field(Of Double)("CaseID") = 25
                   Select row

For Each row In rowsToUpdate
    row.SetField("Status", "Updated Status")
    row.SetField("Date", DateTime.Now.ToShortDateString)
Next

Invoked Arguments:

=> Write Range Workbook dt.
image

Regards

Hi @Ellen ,

Although the solution above with invoke code will work, I don’t think it’s the best method for newcomers, as using Invoke code when you can do the same with UiPath Activities, might be misleading for new developers starting in UiPath.

Another variant from the solution provided in the 1st answer would be:

  1. Save Excel as Datatable (Using Read Range Workbook, as you don’t need formatting)

  2. Find the rows you want to edit.

2.1 - Using LINQ - (Faster but more complex) → dt.AsEnumerable.Where(Function(x) CInt(x("CaseID").ToString.Trim) = 12 OrElse CInt(x("CaseID").ToString.Trim) = 25).AsDataview.ToTable

  • This will keep the rows with ID 12 and 25 in a separate DT, that you can then iterate, and find the index on the original DT. Therefore you are only iterating the number of desired rows, not all of them.
  • You will need to use IndexOf (LINQ - Search data table and return row index) to find the rows on the original DT and update them as explained in 2.2.

2.2 - Using ForEach Row in Datatable Activity - inside the For Each, have an IF, with the following condition (99% similar to post #1) CInt(CurrentRow("CaseID")) = 12 OrElse CInt(CurrentRow("CaseID")) = 25
If True, then use 2x assigns to update the values of Status and Date as follows:

  • CurrentRow("Status") = "Updated"
  • CurrentRow("Date") = Now.ToString("MM/dd/yyyy")
  1. As we have edited the DT, not the Excel, remember to rewrite it, so you get the updated values back in your excel.

Again, both solutions above will work, they are just different in terms of complexity, and ease of understanding. Try to find the best for you :slight_smile:

BR,
Ignasi

1 Like

Thanks for reply I’m trying yours as we dont want to use invoke code in our solutions.

But one thing I need to ask and its… will that only update Stats and DATE on perticular row or will it overright my other data

Is my issue was when i tried with Filter Datatable to filter the row and done the whole process and want to write status and date to the excel back that overwriting my data on orignal file starting with first row…

Your solution giving me error on X value?

With that solution, combining the LinQ and the IndexOf, you will end up with a list of indexes, in your example, it will be 3 and 4, as those are the rows containing 12 or 25.

With that list of indexes, you can edit only that specific Row, for example:

Dt.Rows(var_index).Item(“Status”) = Updated → Where var_index will be 3, then 4, each result of the indexOf.

I’ll quickly prepare a demo workflow and attach it here in a second for your reference.

May I ask, when you say that X is giving an error, can you show me which one?

Thanks!

BR,
Ignasi

Please find attached the workflow:

Input (Sheet1):
image

Output (Sheet2):
image

Code:

Attachments:
DemoInput.xlsx (8.9 KB)
DemoIndexOf.xaml (10.6 KB)

image

its not writing anything to datatable when i use Write Rang workbook as im not reffering anything to table in those assign?
can you help?

unfortunate its same result means its also overwriting my data of row 1- means

|CaseID|ServiceNu|Name|Status|Date

|3|120789|Joe Biden|| Its writing status at this point
|5|120789|Joe Biden||
|9|256437|Donald Trump||it should update this row
|12|256437|Donald Trump|| it should update this row
|25|256437|Donald Trump||it should update this row
|10|352758|Chris Evans||
|35|352758|Chris Evans||
|45|352758|Chris Evans||

which is wrong.

With that workflow, you are editing the CurrentRow, meaning the DataTable variable, that is being iterated.

Your DT will be updated, but the Excel will not, as they are 2 different entities.

If you check my workflow above, or the .xaml file attached

You will see that I’m not editing the CurrentRow, but the Datatable.Rows(IndexGoesHere).Item(“Status”), and I end up my workflow by rewriting the value of the DataTable into the original excel, different sheet.

I’d suggest if you can, to download my attachment, and open it in your project, to use as a guide :slight_smile:

Happy to answer any more questions if any!

BR,
Ignasi

I’m using your first solution…
2.1 - Using LINQ - (Faster but more complex) → querry
varLinqDT = dt.AsEnumerable.Where(Function(x) CInt(x(“CaseID”).ToString.Trim) = 12 OrElse CInt(x(“CaseID”).ToString.Trim) = 25).AsDataview.ToTable`
Then → FOR EACH ROW on that varLinqDT
Then → within FOR each I do 2 assign activity
image
Then out of for each loop I do…
image

Yes, The problem here is that you are using the 1st part, but am I right assuming that you are not using the part of the .IndexOf?

I assume based on your screenshots, that you are iterating the 2 resulting rows from the LINQ, which are basically a DT with 2 rows, and therefore, you are editing that 2 rows, not the original DT.

The problem with that is that you now have the Input DT, and a DT with 2 rows edited, but now you need to mix them.

That can be done with IndexOf, basically finding the index of that 2 rows in your original DT (positions 3 and 4) and then copying your DataRows with edited data in them.

On the shared solution, I did obtain them already with the 1st LINQ:

image

This returns an array with currently 2 positions, {3,4}, as they are the rows that meet my condition.

Then I just need to loop that array, not the DT, and edit the said row. On the 1st iteration, it will edit Row 3, and on the 2nd iteration, it will edit Row 4.

image

That makes it quite easier :slight_smile:

BR,
Ignasi

Thanks I understand now… but I also need the other value from that table/array

let me explain I need the Name out of that as I need to do more compairs after I get the CaseID out.
and when i use this array I can not get the Name out… or can I? if you can explain how?

Actueally My main Excal have the NAME which I dont have anywhere to get this Name out I need to compair the other data from that excel first to be sure that im on right row and then I want the Name out.

You linq of array table is giving me data out but can i get name out of that too?

You can, Did you double-check my solution above? Feel free to download and save it into your project, so you can get some indications, but long story short, this is the input and the output of it:

In short, inside the loop you are editing the Status and the Date Columns, but keeping the rest as is, CaseID, ServiceNu, Name…

Download this file, and open it inside your project ^^

image

I get this issue as well :frowning:
kindly help im stuck with this for 2 days

All good! Can you check if your variable dt_input is of type DataTable? By the error message looks like its not…

Can you paste here the full value of the ¨To¨? so dt_input.Rows(RowIndex…)
Or, if you can, would you mind sharing the workflow so I can take a detailed look at it?

BR,
Ignasi

Test.xlsx (10.3 KB)
TestExcel.xaml (33.3 KB)

I have attached the file kindly have a look.

Check this, is it working for you!
TestExcel.xaml (33.8 KB)

Regards,
Ajay Mishra

image
image

Getting error here.

Can you tell … how can i get Name out of array list?

I did check your code, writing here my findings :slight_smile:

1- Variable name is misleading, dt_input but its of type Array of strings, should be changed to something like arr_int_Indexes, for example, as you will see later on your code that the variable is trying to be used as datatable, but its not.

image

2- 1st assign inside the said ForEach, is also thinking that the RowIndex variable is a DataRow, as it is trying to index it on position 0, but what we have now is a index, not a datarow, so it needs to be remapped as following:

  • Taking into consideration that we are iterating an array of int32, and that this array will have the values 2,4 on it, as those are the ones with CaseID 25 and 12, this is a graphical representation of what we have inside each loop. First loop being the green number, and 2nd group being the blue number.

So Inside each loop, and by having the number, we initially can’t find data in the DataRow, until we say wich Row we want to work on.

In that case, our DataRow will be found by doing the following:

  • dt_Excel as master DataTable
  • RowIndex as Row Index
  • Column name as descriptor of what to find
dt_excel.Rows(RowIndex)("CaseID").ToString

Translated to data:

1st Loop: dt_excel.Rows(2)("CaseID").ToString -> Green Number, CaseID Column.
2nd Loop: dt_excel.Rows(4)("CaseID").ToString -> Blue Number, CaseID Column.
  • 2nd assign would be the same, but with
dt_excel.Rows(RowIndex)("CreaterName").ToString

image

3- Last but not least, the same case on the latest Multiple assign. we need to aim for the master data table (dt_excel), find the desired row (RowIndex), and update the 2 columns’ value (“CaseStatus”, “DateTime”)

  • You had it correct, you just needed to reference to the Excel DataTable, instead of the confusing dt variable you had at the start, and I believe that instead of “CaseStatus”, you want to point to “Status” Column, but might be wrong :slight_smile:

image
image

image
image

4- Extra: You have an “IF” checking if both caseID and SNumb are empty, I guess to skip them, but you are invoking it before filling those variables.

  • If I’m right and you want to “skip” those lines where both values are empty, apply an initial filter instead, and this way you will never have one of this rows going into the ForEach:

Before:
image

After (Keep only rows where those 2 values are not null, or empty):

dt_Excel.AsEnumerable.Where(Function(x) _
	Not String.IsNullOrEmpty(x("CaseID").ToString) AndAlso
	Not String.IsNullOrEmpty(x("ServiceNum").ToString)
	).AsDataView.ToTable
  • Fixed also this LINQ, as it is not working due to variables not being filled previously, and columns not matching your excel columns.
  • “ServiceNumber” = “ServiceNum”

image
image

Added this to test, but they need to be adapted to what you need:
image

image

Find your file attached and corrected, did not test it all as It is using mails :slight_smile:

TestExcel (1).xaml (36.2 KB)
Test.xlsx (10.6 KB)

BR,
Ignasi