Summing

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

How can I sum a range of cells in code. I have tried
myresult=Application.worksheetfunction.sum(Range(cells
(2,1),cells(4,1))

but am having trouble. I get a syntax error.

Thanks!

Kevin
 
Hi Kevin.

Sub Atester04()
Dim rng As Range
Dim myRes As Double

Set rng = Range("A2:A4")

myRes = Application.Sum(rng)
MsgBox myRes

End Sub
 
Thanks for responding. I use the cells construct because
I am actually doing this for more than one 1 column and I
have to do it dynamically because the number of columns
needing summing is not fixed. I actually am using
variables to represent the appropriate cell coordinates.


Thanks!

Kevin
 
Hi Kevin,

Try:

Sub Atester04a()
Dim rng As Range
Dim myRes As Double
Dim i As Long, j As Long, k As Long

i = 2: j = 4: k = 1

Set rng = Range(Cells(i, k), Cells(j, k))
MsgBox rng.Address
myRes = Application.Sum(rng)
MsgBox myRes

End Sub
 
Thanks!
-----Original Message-----
Hi Kevin,

Try:

Sub Atester04a()
Dim rng As Range
Dim myRes As Double
Dim i As Long, j As Long, k As Long

i = 2: j = 4: k = 1

Set rng = Range(Cells(i, k), Cells(j, k))
MsgBox rng.Address
myRes = Application.Sum(rng)
MsgBox myRes

End Sub

---
Regards,
Norman






.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top