How to Add rows one by one and print sum value at the end of the column

Hi, i’m new to Uipath, i have column Amount: 100,25.69,-567.89,89.02…
i want to add all values and display the total at the end of the column,
Note: the values may increase or decrease.
Used Read Range, For Each row, tried while loop didn’t work.
Can you anyone please help

Hi @Sudhir_P ,

There are multiple ways to do it. Some simpler than others. I will try to list out a simpler solution to begin with since you are new to UiPath:

Approach 1:
Read Range into DataTable with Add Headers enabled - I presume that your table begins at column A1

Add a new column to your DataTable using the Add Data Column activity
Let's call this column "LineTotal"

Loop through DataTable using For each row in Data Table activity

For each row in Your_DataTable
Declare a variable xSum as Double
'Do the math using like so:
Assign xSum = Convert.ToDouble(row("0"))+Convert.ToDouble(row("1"))+..etc
'Assign sum to total
row("LineTotal") = xSum
End For

Finally use the Write Range Activity to write Your_DataTable back to the same worksheet, and the output will include the last column “LineTotal”

The flip side of this approach is that you may lose the entire formatting of your worksheet once the Data Table is overwrites your existing worksheet.

Alternately, you can use this approach with a slight modification to write the result directly to a Cell at the end of each line in the Excel. This will not compromise the format of your sheet.

Approach 2:
Read Range into DataTable with Add Headers enabled
Declare a variable x as int32
Assing x=2 (assuming your first data row begins at row 2)

Loop through DataTable using For each row in Data Table activity

For each row in Your_DataTable
Declare a variable xSum as Double
'Do the math using like so:
Assign xSum = Convert.ToDouble(row("0"))+Convert.ToDouble(row("1"))+..etc
'Call Write Cell activity to write xSum to cell Z+x.ToString() for example
' Next row
Assign x = x+1
End For

Hope this gets you started. Also please take the time to go through the Level 1 course on the Academy. It will definitely give you a jump on the learning curve.

Thanks

thank you for reply and clear explanation, i have completed level 1 course in academy, i wish if they had logical exercises too, coming back to my query, my row values may be decrease or increase, how can i apply this logic to it, as it as static.
Assign xSum = Convert.ToDouble(row(“0”))+Convert.ToDouble(row(“1”))+…etc
can you please help me with this ?

Hi again,

Please refer to this recent post. It shows how to navigate the columns horizontally: