Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default Fastest way to find item in an array.

I have an array of 40 items.
I need to go down a column of 600 cells and for each cell I need to
test whether the cell.value is equel to any of the items in the array.
At present this is really slow as my code is doing a loop of 40, 600
times (i.e 24,000).
The loop looks something like:

For i= 1 to 600
myVal = cells(i,1)
For j = 1 to 40
if myVal = myArray(j) then blah blah
next j
next i

Would I have been better using a collection rather than an array?
Is an array the best choice?

Any help greatly appreciated,
J

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Fastest way to find item in an array.

Hi J,

Try something like:

For i = 1 To 600
With Cells(i, 1)
If Not IsError(Application.Match(.Value, myArray, 0)) Then
'do something, e.g:
.Interior.ColorIndex = 6
End If
End With
Next i


---
Regards,
Norman



"WhytheQ" wrote in message
oups.com...
I have an array of 40 items.
I need to go down a column of 600 cells and for each cell I need to
test whether the cell.value is equel to any of the items in the array.
At present this is really slow as my code is doing a loop of 40, 600
times (i.e 24,000).
The loop looks something like:

For i= 1 to 600
myVal = cells(i,1)
For j = 1 to 40
if myVal = myArray(j) then blah blah
next j
next i

Would I have been better using a collection rather than an array?
Is an array the best choice?

Any help greatly appreciated,
J



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 947
Default Fastest way to find item in an array.

Would I have been better using a collection rather than an array?

My personal choice would be a "Dictionary" object, as it has an "Exists"
method.

It would look something like the following instead of a loop.

If Dic.Exists(MyItem) then...
--
HTH. :)
Dana DeLouis
Windows XP, Office 2003


"WhytheQ" wrote in message
oups.com...
I have an array of 40 items.
I need to go down a column of 600 cells and for each cell I need to
test whether the cell.value is equel to any of the items in the array.
At present this is really slow as my code is doing a loop of 40, 600
times (i.e 24,000).
The loop looks something like:

For i= 1 to 600
myVal = cells(i,1)
For j = 1 to 40
if myVal = myArray(j) then blah blah
next j
next i

Would I have been better using a collection rather than an array?
Is an array the best choice?

Any help greatly appreciated,
J



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default Fastest way to find item in an array.

Thanks Norman/Dana, I'll experiment with both of your ideas.
Regards
Jason

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Fastest way to find item in an array.

Dim i as Long, res as Variant
.. . .
For i = 1 to 600
myVal = cells(i,1)
res = Application.Match(myVal,myArray,0)
if not iserror(res) then
' cell matches a value in myarray
else
' cell doesn't match a value in myarray
end if
next i

or just check the values
Dim rng as Range, i as Long, cnt as Long
Dim res as Variant, rng1 as Range
.. . .
set rng = Range("A1:A600")
for i = lbound(myarray) to ubound(myArray)
cnt = Application.Countif(rng,myarray(i)
if cnt 0 then
' there is a match
' if you need to know which cell
res = Application.Match(myarray(i),rng,0)
set rng1 = rng(res)
msgbox "Match at cell " & rng1.Address
end if
Next

--
Regards,
Tom Ogilvy



"WhytheQ" wrote in message
oups.com...
I have an array of 40 items.
I need to go down a column of 600 cells and for each cell I need to
test whether the cell.value is equel to any of the items in the array.
At present this is really slow as my code is doing a loop of 40, 600
times (i.e 24,000).
The loop looks something like:

For i= 1 to 600
myVal = cells(i,1)
For j = 1 to 40
if myVal = myArray(j) then blah blah
next j
next i

Would I have been better using a collection rather than an array?
Is an array the best choice?

Any help greatly appreciated,
J





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 968
Default Fastest way to find item in an array.

Try getting the 600 into an array

dim myVals as variant
myVals=the600Cells
For i= 1 to ubound(myVal,1)
For j = 1 to 40
if myVals(i,1) = myArray(j) then blah blah EXIT FOR
next j
next i

Charles
______________________
Decision Models
FastExcel 2.2 Beta now available
www.DecisionModels.com

"WhytheQ" wrote in message
oups.com...
I have an array of 40 items.
I need to go down a column of 600 cells and for each cell I need to
test whether the cell.value is equel to any of the items in the array.
At present this is really slow as my code is doing a loop of 40, 600
times (i.e 24,000).
The loop looks something like:

For i= 1 to 600
myVal = cells(i,1)
For j = 1 to 40
if myVal = myArray(j) then blah blah
next j
next i

Would I have been better using a collection rather than an array?
Is an array the best choice?

Any help greatly appreciated,
J



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default Fastest way to find item in an array.

Charles,
Thanks for contribution but I think your code is still potentially
going to be looping round i 600 times and for each i there will be 40
j's : in other words 24,000 executions.
Looking at other posts the below code:
For j = 1 to 40
if myVals(i,1) = myArray(j) then blah blah EXIT FOR
next j
could be replaced by:
If Not IsError(Application.Match(.Value, myArray, 0)) Then
'do something
End If
and might be a bit quicker

anyway....thanks everyone!loads of ideas
J

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Fastest way to find item in an array.

You might look carefully again at Charles' suggestion in full, in particular
first putting cell values into an array rather than looping cells.

dim myVals as variant
myVals=the600Cells

Would need to test but I'd be surprised if your Match function works faster
than looping the array.

If you are repetitively searching the same array better first to sort it,
then there are faster methods for searching it.

Regards,
Peter T

"WhytheQ" wrote in message
ups.com...
Charles,
Thanks for contribution but I think your code is still potentially
going to be looping round i 600 times and for each i there will be 40
j's : in other words 24,000 executions.
Looking at other posts the below code:
For j = 1 to 40
if myVals(i,1) = myArray(j) then blah blah EXIT FOR
next j
could be replaced by:
If Not IsError(Application.Match(.Value, myArray, 0)) Then
'do something
End If
and might be a bit quicker

anyway....thanks everyone!loads of ideas
J



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
'auto find average of 4 fastest times' Ditchy Excel Discussion (Misc queries) 3 February 12th 10 10:01 PM
To find rate of each item from item.xls and to copy price.xls pol Excel Discussion (Misc queries) 7 July 16th 09 12:49 AM
fastest sorting routine for 2-D array of long values RB Smissaert Excel Programming 8 May 6th 06 05:06 PM
does an item belong to an array? wcc Excel Programming 4 October 21st 04 08:46 AM
Find fastest way to do lookups Fred Smith Excel Programming 7 April 18th 04 07:44 AM


All times are GMT +1. The time now is 02:54 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"