Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
fra fra is offline
external usenet poster
 
Posts: 4
Default looping through ranges: how to speed up?


Hi all,

I need to loop through 2 ranges (same size, eg A2:A200, B2:B200)
a row at a time, check if the value in the second row is
equal to a specific cell and, if it is, add the value of the
first row to a collection.

I wrote this udf that works fine... but is SLOW!
How could I speed it up?

----------------------------
Public Function foo(rng1 As Range, rng2 As Range, cell) As Long
Dim x As Range
Dim i As Long
Dim unique_cod As New Collection

On Error Resume Next
i = 1
For Each x In rng1
If cell = rng2.Cells(i, 1) Then
'Debug.Print x; " <- "; x.Text; Cstr(x)
unique_cod.Add x, CStr(x)
End If
i = i + 1
Next x

'collection dump
'For i = 1 To unique_cod.Count
' Debug.Print unique_cod(i)
'Next i

foo = unique_cod.Count
End Function
-----------------------------

Thank you in advance, this ng is great!

Best regards,
Francesco
  #3   Report Post  
Posted to microsoft.public.excel.programming
fra fra is offline
external usenet poster
 
Posts: 4
Default looping through ranges: how to speed up?

On Thu, 24 Jun 2004 07:52:18 -0500, Don Guillett wrote:

I guess I'm missing something but wouldn't the COUNTIF function do this?


A B
123 AA
222 BB
123 AA
444 CC
123 FF


How many *different* code I get in column A wich have "AA" in column B? one

I think COUNTIF() would give two.

Thank you
Fra
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,337
Default looping through ranges: how to speed up?

maybe this ARRAY formula to be entered with control+shift+enter instead of
just enter

=COUNT(IF(($B$1:$B$5=B1)*($A$1:$A$5),1))
--
Don Guillett
SalesAid Software

"fra" wrote in message news:opr93sfiq4exgj8n@obi-i-one...
On Thu, 24 Jun 2004 07:52:18 -0500, Don Guillett wrote:

I guess I'm missing something but wouldn't the COUNTIF function do this?


A B
123 AA
222 BB
123 AA
444 CC
123 FF


How many *different* code I get in column A wich have "AA" in column B?

one

I think COUNTIF() would give two.

Thank you
Fra



  #5   Report Post  
Posted to microsoft.public.excel.programming
fra fra is offline
external usenet poster
 
Posts: 4
Default looping through ranges: how to speed up?

On Thu, 24 Jun 2004 13:40:02 -0500, Don Guillett wrote:

maybe this ARRAY formula to be entered with control+shift+enter instead
of
just enter

=COUNT(IF(($B$1:$B$5=B1)*($A$1:$A$5),1))


no, it gives 2 as result for AA.

I think I get the point: passing data from worksheets to VBA and vice
versa is
a s-l-o-w process.

here the explanation:
http://www.avdf.com/apr98/art_ot003.html

thank you anyway
Fra




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default looping through ranges: how to speed up?

I think you will get a 25%-50% improvement if you just change

rng2.Cells(i,1) to rng2(i,1). The .Cells, though a popular syntax for
some reason I have difficulty understanding, is relatively inefficient.

But I think you will get a significantly larger boost if you transfer
the ranges to VBA arrays and loop through the arrays instead of looping
through worksheet ranges, a la

Public Function foo1000(rng1 As Range, rng2 As Range, cell) As Long

Dim y() As Variant, z() As Variant
Dim i As Long
Dim unique_cod As New Collection
y = rng1
z = rng2

On Error Resume Next
i = 1
For Each Elem In y
If cell = z(i, 1) Then
'Debug.Print x; " <- "; x.Text; Cstr(x)
unique_cod.Add Elem, CStr(x)
End If
i = i + 1
Next Elem

'collection dump
'For i = 1 To unique_cod.Count
' Debug.Print unique_cod(i)
'Next i

foo1000 = unique_cod.Count
End Function

Please post back to let us know.

Alan Beban

fra wrote:

I think I get the point: passing data from worksheets to VBA and vice
versa is
a s-l-o-w process.

here the explanation:
http://www.avdf.com/apr98/art_ot003.html

thank you anyway
Fra


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default looping through ranges: how to speed up?

I guess the testing should be done before the posting. The line below
should be

unique_cod.Add Elem, CStr(Elem) rather than unique_cod.Add Elem, CStr(x)

Sorry,
Alan Beban

Alan Beban wrote:
I think you will get a 25%-50% improvement if you just change

rng2.Cells(i,1) to rng2(i,1). The .Cells, though a popular syntax for
some reason I have difficulty understanding, is relatively inefficient.

But I think you will get a significantly larger boost if you transfer
the ranges to VBA arrays and loop through the arrays instead of looping
through worksheet ranges, a la

Public Function foo1000(rng1 As Range, rng2 As Range, cell) As Long

Dim y() As Variant, z() As Variant
Dim i As Long
Dim unique_cod As New Collection
y = rng1
z = rng2

On Error Resume Next
i = 1
For Each Elem In y
If cell = z(i, 1) Then
'Debug.Print x; " <- "; x.Text; Cstr(x)
unique_cod.Add Elem, CStr(x)
End If
i = i + 1
Next Elem

'collection dump
'For i = 1 To unique_cod.Count
' Debug.Print unique_cod(i)
'Next i

foo1000 = unique_cod.Count
End Function

Please post back to let us know.

Alan Beban

fra wrote:


I think I get the point: passing data from worksheets to VBA and vice
versa is
a s-l-o-w process.

here the explanation:
http://www.avdf.com/apr98/art_ot003.html

thank you anyway
Fra


  #8   Report Post  
Posted to microsoft.public.excel.programming
fra fra is offline
external usenet poster
 
Posts: 4
Default looping through ranges: how to speed up?

On Sat, 26 Jun 2004 00:07:45 -0700, Alan Beban wrote:

I guess the testing should be done before the posting. The line below
should be

unique_cod.Add Elem, CStr(Elem) rather than unique_cod.Add Elem, CStr(x)

Sorry,


no problem... and YES it is a boost! the calc time needed was a
"coffee+smoking break" and now is a "check this ng break" :-)

I think this info should be highlight somehow in the manual.

Alan Beban


thank you very much

Ciao
FF
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default looping through ranges: how to speed up?

De nada; thanks for the feedback.

Alan Beban

fra wrote:

On Sat, 26 Jun 2004 00:07:45 -0700, Alan Beban wrote:

I guess the testing should be done before the posting. The line below
should be

unique_cod.Add Elem, CStr(Elem) rather than unique_cod.Add Elem, CStr(x)

Sorry,



no problem... and YES it is a boost! the calc time needed was a
"coffee+smoking break" and now is a "check this ng break" :-)

I think this info should be highlight somehow in the manual.

Alan Beban



thank you very much

Ciao
FF

Reply
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
LOOPING multiple ranges Jase Excel Discussion (Misc queries) 1 April 7th 08 06:21 PM
Looping in VB with cell ranges Freeman Excel Worksheet Functions 2 January 22nd 06 12:14 PM
Looping through named ranges Tim[_38_] Excel Programming 1 May 10th 04 07:20 PM
Looping through Ranges of Rows MS News Excel Programming 6 December 9th 03 06:13 AM
Looping through Ranges of Rows MS News Excel Programming 0 December 8th 03 11:42 AM


All times are GMT +1. The time now is 05:00 PM.

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

About Us

"It's about Microsoft Excel"