Adding all numbers in a given range

  • Thread starter Thread starter Deecrypt
  • Start date Start date
D

Deecrypt

Hi all,
Devising a macro that needs to add all the numbers up together in a
given range of cells i.e Range(ref1 &":"& ref2) and assign the total to
a given variable i.e total. Please let me know how I can do this.
I've tried to record a macro and copy details but that shows absolute
cell references whereas I'm working with relative cell references.
Have also looked thgrough the newsgroups and have not found something
specific to my query.

Thank you kindly
Deecrypt
 
Dim cell As Range
For Each cell In Range(ref1 & ":" & ref2)
If IsNumeric(cell.Value) Then
total = total + cell.Value
End If
Next cell


--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)
 
Thank you Bob, Works like a charm.

Khurram

Bob said:
Dim cell As Range
For Each cell In Range(ref1 & ":" & ref2)
If IsNumeric(cell.Value) Then
total = total + cell.Value
End If
Next cell


--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)
 
To avoid looping through all the cells, try this:

total = WorksheetFunction.Sum(Range(ref1 & ":" & ref2))
 

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