PDA

View Full Version : Worksheet function Sum in Excel VBA



gvreddyhr
08-18-2010, 10:31 PM
Hi,

I have query using worksheet function in excel vba, I have written the code, but Its not getting the results, I am looking some particular columns to do sum and output should be on desired column, since I will be updating the data in same columns in dayin & dayout, below is the example and code which I prepared.

Example:-

A1,B1,C1,D1,E1,F1 - ------ J1(sum of A1,c1,d1,f1)
.
.
.
.
.
A100,b100,c100,d100,e100,f100-------J100(sum of a100,c100,d100,f100)

Code:


Sub Sum_Click()
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Range("J1:J" & lastrow).Formula = Application.WorksheetFunction.Sum(Range("a1:a" & lastrow), Range("c1:c" & lastrow), Range("d1:d" & lastrow), Range("f1:f" & lastrow))
End Sub



I would request any one of you to help me correcting this code.

Thanks in advance.

Regards
GV Reddy

Bob Phillips
08-19-2010, 12:01 AM
Sub Sum_Click()
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Range("J1:J" & lastrow).Formula = "=SUM(A1,C1,D1,F1)"
End Sub