web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Compare 2 excel files ...
Power Automate
Answered

Compare 2 excel files and delete rows that are duplicated

(0) ShareShare
ReportReport
Posted on by 22

Hi Community, 

I am new with PAD and have a problem to address: 

  • 2 excel files (SL and DB)
  • if value of column A from SL file exists in column A from DB file, delete row in DB file.  

This is a Supression List that needs to run in a DataBase excel file, and remove those rows that are in the Supression List. 

I have already created a workflow, but it's not working. 

 

2024-02-23_16h42_40.png

Can you help me?

Thank you in advance!

I have the same question (1)
  • Deenuji_Loganathan_ Profile Picture
    6,250 Moderator on at

    @yellow43 

    We can do this in multiple ways. Below is the PAD way of doing without scripting.

    Flow Screenshot:

    Deenuji_3-1708709284422.png

    Code(Please copy the code below and paste it into your PAD designer; it should work across all language PAD versions):

    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''C:\\Users\\Documents\\SL.xlsx''' Visible: True ReadOnly: False Instance=> ExcelSLInstance
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''C:\\Users\\Documents\\DB.xlsx''' Visible: True ReadOnly: False Instance=> ExcelDBInstance
    Excel.ReadFromExcel.ReadAllCells Instance: ExcelSLInstance ReadAsText: False FirstLineIsHeader: True RangeValue=> ExcelSLData
    Excel.ReadFromExcel.ReadAllCells Instance: ExcelDBInstance ReadAsText: False FirstLineIsHeader: True RangeValue=> ExcelDBData
    LOOP FOREACH CurrentSLItem IN ExcelSLData
    LOOP FOREACH CurrentDBItem IN ExcelDBData
    IF CurrentSLItem = CurrentDBItem THEN
    Excel.FindAndReplace.FindAll Instance: ExcelDBInstance TextToFind: CurrentDBItem[0] MatchCase: False MatchEntireCellContents: False SearchBy: Excel.SearchOrder.Rows Cells=> Cells
    LOOP FOREACH DuplicateCurrentItem IN Cells
    Text.ToNumber Text: DuplicateCurrentItem[1] Number=> NumberToDelete
    Excel.DeleteRow Instance: ExcelDBInstance Index: NumberToDelete
    END
    END
    END
    END
    Excel.SaveExcel.Save Instance: ExcelDBInstance

     

    Results:

    Deenuji_0-1708709698973.png

     

    Thanks,

    Deenu

  • Verified answer
    Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Instead of building excessive loops, which are extremely inefficient, use Find or replace in data table to find if a value in your target file (BD) exists in the value of the origin file (SL).

     

    So, build a single loop to iterate through the rows in the SL line. And then for each row, use Find or replace in data table to find if the BD file contains the current value from the SL line. This action will return a variable called %DataTableMatches% which is a data table with row and column indexes of the matched rows. If this BD file does not contain the value, this table will be empty. So, you can use an If condition to check if %DataTableMatches.RowsCount% equals to 0. If it does, you can use Next loop to skip to the next iteration.

     

    If the matches is not empty, you can then use the row index to delete the row from your Excel file. Now, depending on whether or not you may have more than one match, you may either need to:

    a) build a loop through the matches table to handle each match (if there can be more than one)

    b) simply pick up the first match (if there cannot be more than one).

     

    And then you need a Convert text to number action to convert the index to a numeric value. Get it as %DataTableMatches[0]['Row']% and convert it to a new variable - for example, %RowIndex%. Then use this in the Delete row from Excel action. But make sure you increment it by 2, because indexes are 0-based and ignore the header row in Excel. So, for example, the first row in your table will be at index 0, but it will likely be at row 2 in Excel.

     

    Finally, you also need to make sure to delete the matched row from the data table variable as well, so that the data table matches your Excel file after deleting the row. You can do that by using Delete row from data table to delete the row at %RowIndex% (no need to increase it by 2 in this case).

     

    So, in general, your flow should look like this:

    Agnius_0-1708754887544.pngAgnius_1-1708754931892.png

     

    You can also just copy the entire code below and paste it directly into your flow designer in PAD to have all those actions created automatically for you:

    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''G:\\Documents\\SL_TESTE.xlsx''' Visible: True ReadOnly: False Instance=> SuppressionList
    Excel.GetFirstFreeRowOnColumn Instance: SuppressionList Column: $'''A''' FirstFreeRowOnColumn=> FirstFreeRowOnColumn
    Excel.ReadFromExcel.ReadCells Instance: SuppressionList StartColumn: $'''A''' StartRow: 2 EndColumn: $'''A''' EndRow: FirstFreeRowOnColumn ReadAsText: False FirstLineIsHeader: False RangeValue=> DataSL
    Excel.CloseExcel.Close Instance: SuppressionList
    Variables.RetrieveDataTableColumnIntoList DataTable: DataSL ColumnNameOrIndex: 0 ColumnAsList=> DataSL
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''G:\\Documents\\Extract_TESTE.xlsx''' Visible: True ReadOnly: False Instance=> BD
    Excel.GetFirstFreeRowOnColumn Instance: BD Column: $'''A''' FirstFreeRowOnColumn=> FirstFreeRowOnColumnBD
    Excel.ReadFromExcel.ReadCells Instance: BD StartColumn: $'''A''' StartRow: 2 EndColumn: $'''A''' EndRow: FirstFreeRowOnColumnBD ReadAsText: False FirstLineIsHeader: False RangeValue=> DataBD
    LOOP FOREACH CurrentItem IN DataSL
    Variables.FindOrReplaceInDataTable.FindItemInDataTableByColumnIndex DataTable: DataBD AllMatches: True ValueToFind: CurrentItem MatchCase: False MatchEntireCellContents: True ColumnNameOrIndex: 0 DataTableMatches=> DataTableMatches
    IF DataTableMatches.RowsCount = 0 THEN
    NEXT LOOP
    END
    Text.ToNumber Text: DataTableMatches[0]['Row'] Number=> RowIndex
    Excel.DeleteRow Instance: BD Index: RowIndex + 2
    Variables.DeleteRowFromDataTable DataTable: DataBD RowIndex: RowIndex
    END
    Excel.CloseExcel.CloseAndSave Instance: BD

  • Deenuji_Loganathan_ Profile Picture
    6,250 Moderator on at

    @Agnius - Fantastic approach. Even I don’t know how I missed this approach but always happy to learn from you ❤️

     

    @yellow43 

    Please follow @Agnius suggestion. its looks more efficient and simple one 🤞

  • yellow43 Profile Picture
    22 on at

    Thank you @Agnius for your help!

  • yellow43 Profile Picture
    22 on at

    Thank you @Deenuji for your effort

  • Deenuji_Loganathan_ Profile Picture
    6,250 Moderator on at

    @yellow43 

    Thanks to you, I had the opportunity to learn two distinct methods for removing duplicates in Excel. I appreciate it!

    Wishing you a happy journey with automation!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 1,005

#2
Valantis Profile Picture

Valantis 825

#3
Haque Profile Picture

Haque 619

Last 30 days Overall leaderboard