View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
L. Howard Kittle L. Howard Kittle is offline
external usenet poster
 
Posts: 698
Default Adding Rabges without for each loops

Try this, however I had to name the ranges on the worksheet to make it work.
Tried setting the range in code and it did not work...???

Sub AddRanges()
Dim Cell As Range
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim r1v As Integer
Dim r2v As Integer
Dim r3v As Integer

For Each Cell In Range("rng1")
r1v = r1v + Cell.Value
Next

For Each Cell In Range("rng2")
r2v = r2v + Cell.Value
Next

For Each Cell In Range("rng3")
r3v = r3v + Cell.Value
Next

MsgBox r1v + r2v + r3v

End Sub

HTH
Regards,
Howard

"mdalamers via OfficeKB.com" <u32130@uwe wrote in message
news:6e8b840acd49a@uwe...
Hi,

I should like to add the cells in two ranges WITHOUT having to loop
through
each range separately.
Suppose the cells A1 to A5 in my worksheet are filled, as well as cells B1
to
B5

Now I thought this would do the trick. But apparently not:
Sub AddRanges()
Dim r1 As Range
Dim r2 As Range
Dim r3 As Range

Set r1 = Range("a1:a5")
Set r2 = Range("b1:b5")
Set r3 = Range("c1:c5")

r3.value = r1.value + r2.value
End Sub

Any suggestions?

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200703/1