How do you calculate with code the SUM of a row of data instead of a column of data?
+1
A:
There is no function for this one, you would have to do something like:
select col1, col2, col3, (nz(col1,0)+nz(col2,0)+nz(col3,0)) as Sum from Table
Erich
2010-02-03 16:44:39
Watch out for nulls. 1+Null+1=Null. You can use Nz to get around it.
Remou
2010-02-03 17:27:31
A:
How about this:
Public Function iSum(ParamArray p()) As Variant
Dim i As Long
Dim lngUBound As Long
Dim v As Variant
v = Nz(p(LBound(p)), 0)
lngUBound = UBound(p)
For i = LBound(p) + 1 To lngUBound
If Not IsNull(p(i)) Then
v = v + p(i)
End If
Next
If IsNull(v) Then
v = 0
End If
iSum = v
End Function
And in SQL:
SELECT col1, col2, col3, iSum(col1,col2,col3) As Sum
FROM Table
You can pass any number of columns, and it doesn't matter if any of them are Null. If all are Null, it returns 0.
David-W-Fenton
2010-02-04 00:56:14
Any time the first member of the ParamArray is Null, the function returns zero. That's not your intended behavior, is it?
HansUp
2010-02-04 06:21:46
Bascially, what i have is 5 column headings with information in each column and 5 row headings with info in each row, so its a table of info,,,,so how do i take the "info" in each column but only from row 1 and have it placed in a new seperate cell by itself??
Jeff Anderson
2010-02-04 11:34:39
@HansUp: you're right about that. I've altered the code to address that.
David-W-Fenton
2010-02-05 03:04:01
@Jeff Anderson: it seems to me that @Remou's answer does the job, as would mind. Maybe you haven't properly explained the context, because both answers look to me to completely solve the question as you asked it.
David-W-Fenton
2010-02-05 03:05:43