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 / Delete rows from creat...
Power Automate
Answered

Delete rows from created excel file and add data to existing main file

(0) ShareShare
ReportReport
Posted on by 30

Hi,

 

The Background: I have a desktop flow which pulls downloads an excel file and saves it to my local drive. I receive similar file each day (same number and title of columns) which, so far, I have been able to save using a datestamp for each file. 

 

Context Reference: As of now, this is the code I have been using to execute my flow successfully

 

Outlook.Launch Instance=> OutlookInstance
Outlook.RetrieveEmailMessages.RetrieveEmailsAndSaveAttachments Instance: OutlookInstance Account: $'''xyz@outlook.com''' MailFolder: $'''Inbox''' EmailsToRetrieve: Outlook.RetrieveMessagesMode.Unread MarkAsRead: True ReadBodyAsHtml: False SubjectContains: $'''XYZ Report''' SaveAttachmentsInto: $'''Z:\\XYZ\\ABC''' Messages=> RetrievedEmails
LOOP FOREACH CurrentEmail IN RetrievedEmails
 LOOP FOREACH CurrentAttachment IN CurrentEmail.Attachments
 IF Contains(CurrentAttachment.Extension, $'''xlsx''', False) THEN
 DateTime.GetCurrentDateTime.Local DateTimeFormat: DateTime.DateTimeFormat.DateAndTime CurrentDateTime=> CurrentDateTime
 Text.ConvertDateTimeToText.FromCustomDateTime DateTime: CurrentDateTime CustomFormat: $'''MMddyyyy''' Result=> FormattedDateTime
 File.RenameFiles.Rename Files: CurrentAttachment NewName: $'''%CurrentAttachment.NameWithoutExtension%_%FormattedDateTime%%CurrentAttachment.Extension%''' KeepExtension: True IfFileExists: File.IfExists.Overwrite RenamedFiles=> RenamedFiles
 LOOP FOREACH CurrentItem IN RenamedFiles
 Excel.LaunchExcel.LaunchAndOpen Path: CurrentItem Visible: True ReadOnly: False LoadAddInsAndMacros: False Instance=> ExcelInstance
 DISABLE Excel.SetActiveWorksheet.ActivateWorksheetByName Instance: ExcelInstance Name: RenamedFiles
 # Set the correct Sheet name here from where you want to delete the rows.
 SET RowCounter TO 1
 LOOP WHILE (RowCounter) <= (3)
 Excel.DeleteRow Instance: ExcelInstance Index: 1
 Variables.IncreaseVariable Value: RowCounter IncrementValue: 1
 END
 Excel.CloseExcel.CloseAndSaveAs Instance: ExcelInstance DocumentFormat: Excel.ExcelFormat.FromExtension DocumentPath: $'''Z:\\XYZ\\ABC\\Dated Reports\\XYZ_report%FormattedDateTime%'''
 END
 END
 END
END
File.Delete Files: RenamedFiles

 

 

The Problem: The requirements have changed recently where I would like to add the data from the file that I am receiving each day into one main file. For this purpose, I would have to essentially delete the last two rows from the main file (which contains values like 'total' etc.) and then delete the first row from the new file (which would contain the title of the columns as they would not be needed if the titles are already present in the main file) and finally add the remaining data from the most recent file for download. 

 

Anticipated Solution: I would like to add to my flow so that similar to my current flow, the last two four rows are deleted from the main file on my drive, the first four rows are deleted from the new file being downloaded, and finally the data from the new file is added under the existing rows of my main file.

 

TIA!

 

 

 

I have the same question (0)
  • Riyaz_riz11 Profile Picture
    4,214 Super User 2026 Season 1 on at

    Hi @Humanoidhuman 

    @@copilotGeneratedAction: 'False'
    Database.ExecuteSqlStatement.ConnectAndExecute ConnectionString: $'''Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%strDataFolderPath%\\BusinessConfig.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=YES\";''' Statement: $'''SELECT * FROM [Config$]''' Timeout: 30 Result=> tblConfigData
    Excel.LaunchExcel.LaunchUnderExistingProcess Visible: True Instance=> ExcelInstance
    Excel.WriteToExcel.WriteCell Instance: ExcelInstance Value: tblConfigData Column: 1 Row: 1
    Excel.CloseExcel.CloseAndSaveAs Instance: ExcelInstance DocumentFormat: Excel.ExcelFormat.FromExtension DocumentPath: $'''D'''
    

     

    You can try with action in select query you can specify the required columns, and even it is faster than excel.

    Instead of data folder path you can use --for each current item

     

     

    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    Regards,
    Riyaz

  • CU31080729-3 Profile Picture
    502 Moderator on at

    Hi,

    if I properly understand you question then fallow step as per your requirment 

    1) after downloading your new file then launch this excel (Instance1) file and used get first free column/row action and find out the free last row (%FirstFreeRow1%) , on the basis of this free last row you can delete your "total" containing last row (total containing row =%FirstFreeRow1%-1) and delete the first row by using delete row from excel worksheet action and provide input your header row (ie. 1).

    2) after deleting header and last row then read all data from this excel file (Data1).

    3) then open your main excel file (Instance2) and used get first free column/row action and find out the free last row (%FirstFreeRow2%) of this excel file and write your Data1 into your main file by using write excel to excel worksheet into on specific cell Column A and row is %FirstFreeRow2%.

     

    I hope this solution work for you.

     

    (Note:- if you got your solution you can mark as solution and gives kudos)

     

    Thanks & Regards

    Chetan Ninghot

  • Verified answer
    Humanoidhuman Profile Picture
    30 on at

    Thank you @Ahammad_Riyaz and @Chetan_Ninghot .

    @Ahammad_Riyaz I cannot say for certain if your method would have enabled me to execute my requirements for fulfilment due my my limited knowledge regarding SQL code.
    @Chetan_Ninghot your insight provided me to figure out the way where I needed to get. Although the only issue I would say is trying to execute "%FirstFreeRow%-1" generated an error for me.

    However, I am posting what led me to the solution that I needed [based on ideas from different places]. Needless to say there would definitely be easier and cleaner ways to execute which I will be working on. 
    Also, just FYI I manually created the 'Main' file as I had to include the data from the previous days into it (as my flow has been set to run on a daily basis when the new email arrives with the new dataset). 

    Plus, the last bit was inserted in order to delete the empty rows which were created once the new data had been placed within the Main excel file. 
    I am including my flow in HTML format below in case anyone wants to replicate something similar. 

    Outlook.Launch Instance=> OutlookInstance
    Outlook.RetrieveEmailMessages.RetrieveEmailsAndSaveAttachments Instance: OutlookInstance Account: $'''XYZ@outlook.com''' MailFolder: $'''Inbox''' EmailsToRetrieve: Outlook.RetrieveMessagesMode.Unread MarkAsRead: True ReadBodyAsHtml: False SubjectContains: $'''XYZ Report''' SaveAttachmentsInto: $'''Z:\\xyz\\abc''' Messages=> RetrievedEmails
    LOOP FOREACH CurrentEmail IN RetrievedEmails
     LOOP FOREACH CurrentAttachment IN CurrentEmail.Attachments
     IF Contains(CurrentAttachment.Extension, $'''xlsx''', False) THEN
     DateTime.GetCurrentDateTime.Local DateTimeFormat: DateTime.DateTimeFormat.DateAndTime CurrentDateTime=> CurrentDateTime
     Text.ConvertDateTimeToText.FromCustomDateTime DateTime: CurrentDateTime CustomFormat: $'''MMddyyyy''' Result=> FormattedDateTime
     File.RenameFiles.Rename Files: CurrentAttachment NewName: $'''%CurrentAttachment.NameWithoutExtension%_%FormattedDateTime%%CurrentAttachment.Extension%''' KeepExtension: True IfFileExists: File.IfExists.Overwrite RenamedFiles=> RenamedFiles
     LOOP FOREACH CurrentItem IN RenamedFiles
     Excel.LaunchExcel.LaunchAndOpen Path: CurrentItem Visible: True ReadOnly: False LoadAddInsAndMacros: False Instance=> ExcelInstance
     DISABLE Excel.SetActiveWorksheet.ActivateWorksheetByName Instance: ExcelInstance Name: RenamedFiles
     # Set the correct Sheet name here from where you want to delete the rows.
     SET RowCounter TO 1
     LOOP WHILE (RowCounter) <= (4)
     Excel.DeleteRow Instance: ExcelInstance Index: 1
     Variables.IncreaseVariable Value: RowCounter IncrementValue: 1
     END
     Excel.CloseExcel.CloseAndSaveAs Instance: ExcelInstance DocumentFormat: Excel.ExcelFormat.FromExtension DocumentPath: $'''Z:\\xyz\\abc\\Dated Reports\\XYZ_report%FormattedDateTime%'''
     END
     END
     END
    END
    File.Delete Files: RenamedFiles
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''Z:\\xyz\\abc\\XYZ_report_Main.xlsx''' Visible: False ReadOnly: False Instance=> MainFile
    Excel.SetActiveWorksheet.ActivateWorksheetByName Instance: MainFile Name: $'''XYZReport'''
    Excel.GetFirstFreeColumnRow Instance: MainFile FirstFreeColumn=> FirstFreeColumn FirstFreeRow=> FirstFreeRow
    LOOP LoopIndex FROM 1 TO FirstFreeRow STEP 1
     Excel.ReadFromExcel.ReadCell Instance: MainFile StartColumn: 1 StartRow: LoopIndex ReadAsText: False CellValue=> ExcelData
     IF ExcelData = $'''%''%''' THEN
     Excel.DeleteRow Instance: MainFile Index: LoopIndex
     END
    END
    LOOP LoopIndex FROM 1 TO FirstFreeRow STEP 1
     Excel.ReadFromExcel.ReadCell Instance: MainFile StartColumn: 1 StartRow: LoopIndex ReadAsText: False CellValue=> ExcelData
     IF ExcelData = $'''%''%''' THEN
     Excel.DeleteRow Instance: MainFile Index: LoopIndex
     END
    END
    Excel.CloseExcel.CloseAndSave Instance: MainFile
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''Z:\\xyz\\abc\\XYZ_report_Main.xlsx''' Visible: True ReadOnly: False Instance=> ExcelInstance3
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''Z:\\xyz\\abc\\Dated Reports\\XYZ_report%FormattedDateTime%.xlsx''' Visible: True ReadOnly: False Instance=> ExcelInstance2
    Excel.ReadFromExcel.ReadAllCells Instance: ExcelInstance2 ReadAsText: False FirstLineIsHeader: False RangeValue=> ExcelData2
    Excel.WriteToExcel.WriteCell Instance: ExcelInstance3 Value: ExcelData2 Column: 1 Row: FirstFreeRow
    Excel.CloseExcel.Close Instance: ExcelInstance2
    Excel.CloseExcel.CloseAndSave Instance: ExcelInstance3
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''Z:\\xyz\\abc\\XYZ_report_Main.xlsx''' Visible: False ReadOnly: False Instance=> MainDocument
    Excel.SetActiveWorksheet.ActivateWorksheetByName Instance: MainDocument Name: $'''XYZReport'''
    Excel.GetFirstFreeColumnRow Instance: MainDocument FirstFreeColumn=> FirstFreeColumn1 FirstFreeRow=> FirstFreeRow1
    LOOP LoopIndex1 FROM 1 TO FirstFreeRow1 STEP 1
     Excel.ReadFromExcel.ReadCell Instance: MainDocument StartColumn: 1 StartRow: LoopIndex1 ReadAsText: False CellValue=> ExcelData3
     IF ExcelData3 = $'''%''%''' THEN
     Excel.DeleteRow Instance: MainDocument Index: LoopIndex1
     END
    END
    LOOP LoopIndex1 FROM 1 TO FirstFreeRow1 STEP 1
     Excel.ReadFromExcel.ReadCell Instance: MainDocument StartColumn: 1 StartRow: LoopIndex1 ReadAsText: False CellValue=> ExcelData3
     IF ExcelData3 = $'''%''%''' THEN
     Excel.DeleteRow Instance: MainDocument Index: LoopIndex1
     END
    END
    Excel.CloseExcel.CloseAndSave Instance: MainDocument

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 252 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 169

#3
Haque Profile Picture

Haque 154

Last 30 days Overall leaderboard