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 / Unable to Conditionall...
Power Automate
Answered

Unable to Conditionally Add New Worksheets

(2) ShareShare
ReportReport
Posted on by 15
I have a folder with .csv files - new files are created daily and are named with the date, e.g. 2024-09-01.  I've written a flow that gets a list of the files in the folder and loads them into individual worksheets in Excel.  The worksheets are named with the file name.  I'm trying to find a way to have the flow check whether there is already a worksheet with that name and if so, skip to the next file in the list.  I've tried a bunch of different ways to do this but none of them work.  I've tried using Set active Excel worksheet to the current filename and using On Error and a label to cause the flow to jump past the Add New Worksheet and Write to Excel Worksheet.  But Excel still creates blank worksheets (Sheet1, Sheet2, and so on) for each instance of the files.  I've tried deleting the "Sheetx" worksheet as soon as it's created but that doesn't work.  How can I accomplish this?
  • Suggested answer
    Nived_Nambiar Profile Picture
    18,138 Super User 2026 Season 1 on at
    Hi,
     
    Try this approach, you have to list the sheets present in the final file where you need to append sheets and loop through each file to check whether sheet exists with file name or not. see the demo flow designed for this 
     
     
     
     
    Here Final.xlsx is the file where we need to append the sheet from each file which is present in Input (as per the flow)
     
    copy paste the same flow from below
     
    System.TerminateProcess.TerminateProcessByName ProcessName: $'''EXCEL'''
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''D:\\Temp\\PowerappsLearning\\Final.xlsx''' Visible: False ReadOnly: False Instance=> ExcelInstance2
    Excel.GetAllWorksheets Instance: ExcelInstance2 Worksheets=> SheetNames
    Folder.GetFiles Folder: $'''D:\\Temp\\PowerappsLearning\\Input''' FileFilter: $'''*''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> Files
    LOOP FOREACH File1 IN Files
        Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: File1 Visible: False ReadOnly: False Instance=> ExcelInstance3
        Excel.GetActiveWorksheet Instance: ExcelInstance3 Name=> InputSheetName
        IF NotContains(SheetNames, File1.NameWithoutExtension, False) THEN
            Excel.CopyExcelWorksheet.CopyWorksheetWithName SourceInstance: ExcelInstance3 SourceWorksheetName: InputSheetName TargetInstance: ExcelInstance2 TargetWorksheetName: File1.NameWithoutExtension PasteWorksheetPosition: Excel.WorksheetPosition.Last
        END
        Excel.CloseExcel.Close Instance: ExcelInstance3
    END
    Excel.CloseExcel.CloseAndSave Instance: ExcelInstance2
    
     
     
    Thanks & Regards,
    Nived N 
    LinkedIn: Nived N's LinkedIn
    YouTube: Nived N's YouTube Channel
    Blog: Nived Nambiar's Blogs
     Found my answer helpful? Please consider marking it as the solution!
     Your appreciation keeps me motivated. Thank you!

     

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

Leaderboard > Power Automate

#1
Mohsin Ali Profile Picture

Mohsin Ali 445

#2
11manish Profile Picture

11manish 345 Super User 2026 Season 2

#3
M Bilal Khan Profile Picture

M Bilal Khan 300

Last 30 days Overall leaderboard