LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default Adding Rabges without for each loops

Well, it should be a time-to-execute thing, not a number-of-lines thing. The
line count is pretty close. Looping through ranges can be very slow, of
course, but if you read the ranges into arrays, and write the results as an
array, it will avoid the slow cell-by-cell looping. It might even be faster
than Dave's interesting technique, because his has at least one extra
worksheet operation.

Sub AddRanges()
Dim v1 As Variant
Dim v2 As Variant
Dim v3(1 to 5, 1 to 1) As Variant
Dim i As Integer

' get worksheet data into VB arrays
v1 = Range("a1:a5").Value
v2 = Range("b1:b5").Value

' loop VB arrays to do the addition
For i = 1 to 5
v3(i, 1) = v1(i, 1) + v2(i, 1)
Next

' write VB array back to sheet
Range("c1:c5").Value = v3

End Sub

Slightly shorter, one less trip to the worksheet, but reading is faster than
writing:

Sub AddRanges2()
Dim vIn As Variant
Dim vOut(1 to 5, 1 to 1) As Variant
Dim i As Integer

' get worksheet data into VB arrays
vIn = Range("a1:b5").Value

' loop VB arrays to do the addition
For i = 1 to 5
vOut(i, 1) = vIn(i, 1) + vIn(i, 2)
Next

' write VB array back to sheet
Range("c1:c5").Value = vOut

End Sub

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"mdalamers via OfficeKB.com" <u32130@uwe wrote in message
news:6e952ee1953d3@uwe...
Thanks Guys.

This helped.
By the way Jon. Nothing is wrong with looping but I prefer using a few
lines
rather than many lines.
Have a great weekend!

Michiel

--
Message posted via http://www.officekb.com



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loops fugfug[_2_] Excel Programming 3 July 8th 05 10:53 AM
Loops Snow[_2_] Excel Programming 2 May 13th 04 09:48 PM
Loops etc. Jonathan Vickers Excel Programming 6 February 28th 04 05:35 PM
help with loops Rick B[_6_] Excel Programming 8 January 28th 04 12:32 AM
LOOPS Fernando Duran Excel Programming 2 September 3rd 03 01:07 AM


All times are GMT +1. The time now is 09:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"