Comparing 2 excel files, finding the last matching row, and then copying the additional rows from to the other file

Hi All, I’m trying to develop an automation where I’m comparing 2 files - File 1 and File 2. File 1 has a list of transactions, File 2 has the same list - plus new transactions. They are formatted the same way. I need to find the last matching row based on 3 out of 4 columns, and then only copy the newest rows from File2 to File1.

Currently I’m finding the last value in each column and saving it to a variable in File1 then have in If Statement for File2- If each column is equal to the variables of the last row in File2, then Set another variable to CurrentIndex.

Then for each row in File 1 if CurrentIndex>IndexofLastRow write each cell. However, instead it’s writing each row and resulting in duplicates

Hi,

Can you share input sample and expected output?

Regards,

Hi @sjavits-cohan

Can you share your both input excel files and expected Output.

Regards

I can share test files. See attached. File ‘Test’ has a few transactions, File ‘Test2’ has the same transactions, but also new ones. I need the robot to find the last row on ‘Test’, find the matching row based on Columns A, C & D on ‘Test2’ and add any following rows from ‘Test2’ to ‘Test’. Currently, the bot adds ALL lines from ‘Test2’ to ‘Test’ resulting in duplicates.
Test.xlsx (9.2 KB)
Test2.xlsx (9.3 KB)

I can share test files. See attached. File ‘Test’ has a few transactions, File ‘Test2’ has the same transactions, but also new ones. I need the robot to find the last row on ‘Test’, find the matching row based on Columns A, C & D on ‘Test2’ and add any following rows from ‘Test2’ to ‘Test’. Currently, the bot adds ALL lines from ‘Test2’ to ‘Test’ resulting in duplicates.

Test.xlsx (9.2 KB)
Test2.xlsx (9.3 KB)

Hi,

Can you also share expected output?

Does the following sample work for you?

Sample
Sample20240109-1.zip (23.6 KB)

Regards,

Hi @sjavits-cohan

=> Use Read Range Workbook to read the excels and store both the excel outputs in a datatable say dt_Test and dt_Test2.
Input:


Output: dt_Test

Output: dt_Test2
=> Use the below code in Invoke Code activity:

' Get the last row from 'Test'
Dim lastRowTest As DataRow = Test.AsEnumerable().LastOrDefault()

' Iterate through each row in 'Test2'
For Each rowTest2 As DataRow In Test2.Rows
    ' Check for matching rows in Columns A, C, and D
    Dim matchingRows = Test.AsEnumerable().Where(
        Function(x) x("Date").ToString() = rowTest2("Date").ToString() AndAlso
                    x("Amount").ToString() = rowTest2("Amount").ToString() AndAlso
                    x("Code").ToString() = rowTest2("Code").ToString()
    )

    ' If no matching rows found, add the row from 'Test2' to 'Test'
    If matchingRows.Count() = 0 Then
        Dim newRow As DataRow = Test.NewRow()
        newRow("Date") = rowTest2("Date")
        newRow("Amount") = rowTest2("Amount")
        newRow("Code") = rowTest2("Code")
        Test.Rows.Add(newRow)
    End If
Next

Below are the Invoked arguments:

=> Use Write Range Workbook to write the dt_Test datatable back to excel in a different Sheet.
Output:

Workflow:


xaml:
Sequence11.xaml (10.3 KB)

Hope it helps!!
Regards

Hi @Parvathy is this possible to do in StudioX?

Yes it’s possible to do @sjavits-cohan

I will give you the workflow for that. Give me some time.

Regards

Great, thank you!!!

Hi @sjavits-cohan

Please find the below zip file in Studio X.
Test.xlsx and Test2.xlsx are the input excels provided by you.
Output.xlsx is the output excel.

Output:

Workflow:

FORUM EXCEL TASK.zip (185.6 KB)

Happy to help again if any queries.

Regards

This worked, thank you!

1 Like

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