|
-
May 23rd, 2006, 03:31 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Working with Array VBA Excel
Is there a faster way to find the index of a value contained in an array than looping the entire array and compare the value you want to find to the value of the index???
EX:
VB Code:
For lnCounter = 0 To UBound(TheArray, 2)
If MyValue = TheArray(lnCounter,1) then
MyIndex = lnCounter
End If
Next lnCounter
Thanks
-
May 23rd, 2006, 03:59 PM
#2
Re: Working with Array VBA Excel
No there isn't a better way, but you could optimize your code by including an EXIT FOR statement within your IF...THEN. That way you only loop through the array until you find the value.
VB Code:
For lnCounter = 0 To UBound(TheArray, 2)
If MyValue = TheArray(lnCounter, 1) Then
MyIndex = lnCounter
Exit For
End If
Next lnCounter
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
May 24th, 2006, 03:37 AM
#3
Re: Working with Array VBA Excel
If your array is already sorted you can do the search which moves by halves.
ie
1
2
3
4
5
6
7
You look for 5.
The program first moves to the half way point (4) says its looking for greater than this.
It then looks at the next 4, divides in half again, and checks 6
The program now says its looking for lower, so between positions 4 and 6.
Then it halves the remaining list items and returns 5.
There are other methods too.
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
May 24th, 2006, 08:17 AM
#4
Thread Starter
Addicted Member
Re: Working with Array VBA Excel
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|