Skip to main content

Notifications

Community site session details

Community site session details

Session Id : Z+WhgR8Y2bNCFgNJNtgm1X
Power Automate - Power Automate Desktop
Answered

Split Excel Rows when writing to Excel worksheet

Like (2) ShareShare
ReportReport
Posted on 22 Sep 2024 20:04:11 by 15

Issue: Incorrect row duplication and error with Write to Excel Worksheet in Power Automate Desktop

I’m working on a Power Automate Desktop flow that processes Excel data, which checks for payment due to citizens. The output spreadsheet cannot have a value of more than £100 per row so it splits payment values into increments of £100. Anyone due less than £100 just has their row of data copied across by writing these values into a new Excel sheet, anyone with an amount over £100, for example £250, would have three rows of data. Two for £100 each and one for £50, before the loop moves on to the next person. I'm encountering multiple problems:

  1. Row Duplication/Deletion:

    • Payments greater than £100 (like £200) are being split, but rows are either getting duplicated or missing.
    • Sometimes, rows with payments under £100 are being deleted entirely from the result file.
    • I also encounter blank rows in the output.
  2. Error in Write to Excel Worksheet Action:

    • When using the ExcelRowCounter variable to specify the row for writing to Excel, I get the error "Argument 'Row' must be an integer value."
    • If I manually replace ExcelRowCounter with a specific number (e.g., 2), the error disappears but the row sequencing doesn’t work correctly.
  3. Flow Logic:

    • I’m using If statements and a loop (LoopIndex) to handle payments over £100 and increment the ExcelRowCounter. However, there seems to be a problem with the loop logic or how ExcelRowCounter is managed.
    • I’m not sure if the LoopIndex and NumberOfFullPayment conditions are correctly placed, leading to the wrong rows being written.
  4. Expected Output:

    • For amounts over £100, I need the payment split into £100 increments with a new row for each £100 and the remainder, if any (e.g., a payment of £120 should produce one row for £100 and one for £20).

      Here is a copy of my flow:

      Any advise would be much appreciated. I have tried moving around the Loops but getting nowhere. Many thanks in advance. Mark
       
 
Categories:
  • MarkLennon01 Profile Picture
    15 on 23 Sep 2024 at 13:05:52
    Split Excel Rows when writing to Excel worksheet
    Hi Vishnu
    Thanks for getting back to me. However, I need to entirely resolve via Power Automate Desktop as we cannot use VBA in our work. And personally have no experience of it too.
    Best wishes
    Mark
  • Verified answer
    VishnuReddy1997 Profile Picture
    2,324 Super User 2025 Season 1 on 23 Sep 2024 at 04:57:59
    Split Excel Rows when writing to Excel worksheet
    Hey Hi,
     
    Try this VBscript.Please copy the below Vbscript code in your Run Vbscript action in PAD and Change the paths accordingly.
     
    Option Explicit
    
    ' Create Excel application object
    Dim objExcel, objWorkbook, objSheet, objNewWorkbook, objNewSheet
    Dim lastRow, i, row, paymentAmount, remainingAmount
    
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = False ' Make Excel visible if you want to debug
    
    ' Open the input Excel file
    Set objWorkbook = objExcel.Workbooks.Open("C:\path\to\input\file.xlsx")
    Set objSheet = objWorkbook.Sheets(1)
    
    ' Create a new Excel workbook for the output
    Set objNewWorkbook = objExcel.Workbooks.Add
    Set objNewSheet = objNewWorkbook.Sheets(1)
    
    ' Copy header from original sheet to new sheet
    For i = 1 To objSheet.UsedRange.Columns.Count
        objNewSheet.Cells(1, i).Value = objSheet.Cells(1, i).Value
    Next
    
    ' Get the last row of the input data
    lastRow = objSheet.UsedRange.Rows.Count
    
    ' Start processing from the second row (assuming first row is header)
    row = 2
    For i = 2 To lastRow
        ' Get the payment amount from the specific column (assuming it's column 2 here)
        paymentAmount = objSheet.Cells(i, 2).Value
        
        ' Check if payment is less than or equal to £100
        If paymentAmount <= 100 Then
            ' Copy the entire row to the new sheet
            objNewSheet.Rows(row).Value = objSheet.Rows(i).Value
            row = row + 1
        Else
            ' Process payment by splitting into increments of £100
            remainingAmount = paymentAmount
            
            Do While remainingAmount > 0
                If remainingAmount >= 100 Then
                    ' Copy the row and set payment to £100
                    objNewSheet.Rows(row).Value = objSheet.Rows(i).Value
                    objNewSheet.Cells(row, 2).Value = 100 ' Change payment to £100
                    remainingAmount = remainingAmount - 100
                    row = row + 1
                Else
                    ' Copy the row and set payment to the remaining amount
                    objNewSheet.Rows(row).Value = objSheet.Rows(i).Value
                    objNewSheet.Cells(row, 2).Value = remainingAmount ' Set the remaining payment
                    remainingAmount = 0
                    row = row + 1
                End If
            Loop
        End If
    Next
    
    ' Save the new workbook
    objNewWorkbook.SaveAs "C:\path\to\output\file.xlsx"
    
    ' Close workbooks and quit Excel
    objWorkbook.Close False
    objNewWorkbook.Close True
    objExcel.Quit
    
    ' Clean up
    Set objSheet = Nothing
    Set objWorkbook = Nothing
    Set objNewSheet = Nothing
    Set objNewWorkbook = Nothing
    Set objExcel = Nothing
    
    WScript.Echo "Processing complete."
    
     
     
    (Note:- if you got your solution you can mark as solution and gives kudos)
     
    Thanks & Regards
    Vishnu Reddy

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - Power Automate Desktop

#1
eetuRobo Profile Picture

eetuRobo 18 Super User 2025 Season 1

#2
stampcoin Profile Picture

stampcoin 6

#2
John_Mavridis Profile Picture

John_Mavridis 6 Microsoft Employee

Overall leaderboard
Loading started