If you want to ensure that the last row of the DataTable (which contains the previous day’s total) is deleted before you append the new entry and calculate the daily sum, you can follow these steps:
Steps to Remove the Last Row
Check If the DataTable is Not Empty: First, you need to check if the DataTable has rows. This will prevent errors if the DataTable is empty.
Remove Last Row: Use the RemoveAt method on the DataTable’s Rows collection to remove the last row.
Continue with Your Process: After removing the last row, proceed with appending the new count and calculating the new sum.
Example Implementation
Here’s how you can structure your UiPath workflow to remove the last row before appending the new count:
1. Check if DataTable Contains Rows
Before removing the last row, you should check if your masterDataTable has any rows. You can use an If activity for this:
If masterDataTable.Rows.Count > 0
{
// Remove the last row
masterDataTable.Rows.RemoveAt(masterDataTable.Rows.Count - 1)
}
2. Append New Count Entry
After potentially removing the last row, you can proceed to append the new count as described in your previous workflow:
DataRow newRow = masterDataTable.NewRow()
newRow("CountColumnName") = newCount ' Replace "CountColumnName" with your actual column name
masterDataTable.Rows.Add(newRow)
3. Calculate Daily Sum
Now, perform the sum calculation:
totalCount = masterDataTable.AsEnumerable().Sum(Function(row) Convert.ToInt32(row("CountColumnName"))) ' Adjust for the correct type
Finally, write the modified DataTable back to the master sheet:
Excel Application Scope (Path to your master Excel file)
{
Write Range - Input: masterDataTable, Range: "A1" ' Adjust the range as necessary
}
Full Sequence Overview
Here’s a simplified version of the full workflow:
Excel Application Scope - Open master file.
Read Range → masterDataTable
IfmasterDataTable.Rows.Count > 0
Then → masterDataTable.Rows.RemoveAt(masterDataTable.Rows.Count - 1)
Create New DataRow → newRow and append it to masterDataTable
Calculate Sum → totalCount
Create Sum DataRow → sumRow with totalCount
Write Range to master file
Important Notes
Data Integrity: Ensure that the DataTable is not empty before trying to remove the last row to avoid an IndexOutOfRange exception.
Count Column: Replace "CountColumnName" with the actual name of your count column.
Error Handling: Consider adding error handling (like Try-Catch) to gracefully manage any unexpected situations, such as issues with the Excel file or data types.
Because after appending it will write Totals summary. see screenshot in top. If I can’t delete that one than append activity will append and calculation will be wrong