Getting the sum of certain values in a column

image

How to get the sum of certain values in a column? Thank you for the help.

1 Like

Method1: Using read range to get the data and sum of these.
Mehod2: Invoke vnbet code to calculate the sum of speicific column

Should I use vnbet code? Is there any other way?

Using vnbet is more quicly i think, Also you can read range of column and loop the datatable to get sum.

Hi @ROBERT_RUSSELL_MONSA ,

I’ll suggest you to use following LinQ code,

Sum = (DT.AsEnumerable().Sum(Function(i) Convert.ToDouble(i.Field(Of Double)("Regular Hours").ToString)))
2 Likes

Can you please explain what is happening in this code , like what each keyword stands for etc @samir

@iVishalNayak

Yeah sure,

here, DT.AsEnumerable() is a method converts in generic collection or you can say object which can be used in a Linq expression.
Basically, it allows you iterate through like for each row activity.

Sum(Function(i) Convert.ToDouble(i.Field(Of Double)("Regular Hours").ToString))

here, .Sum is aggregation function, which is used to calculate a single sum value from the collection of the values.

i.Field(Of Double)(“Regular Hours”)
—> this is getting values from column “Regular Hours”

i.Field(Of Double)
—> specifying that value is of type double and if you not
mention it there, anyway by Convert.ToDouble is converting it’s in double for applying .Sum function on it.

1 Like

Yes there is many ways to achieve this if you want then you can simple store this in a dataTable and then usin For Each Row activity then using assign Sum=Convert.ToDouble(row(“Column_Name”).ToString)+Sum
gives the sum of that column

4 Likes

If you can make sure all values in your excel column is numeric, then after the read range you can do:
mySum = DataTable.Compute(“SUM([Regular Hours]),”")

2 Likes

Thanks a lot @samir

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.