LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Using an Array instead of a Vlookup to improve speed

It will depend on the particular situation, but I think doing a lookup via
an array loop is much faster.
This is an example of an array function doing a lookup, that you can adapt
to suit your situation.

Function ArrayLookup(ByVal lLookupValue As Long, _
ByRef arr As Variant, _
ByRef lCol1 As Long, _
ByRef lCol2 As Long, _
Optional bReplace As Boolean = False, _
Optional ByRef strReplace As String = "any", _
Optional ByRef strReplace2 As String = "") As String

'will do a lookup in a 2-D array
'the lookup value has to be an integer number
'the result will have to be a string value
'optionally will replace a found value with another string
'---------------------------------------------------------
Dim LB As Byte
Dim UB As Long
Dim i As Long

LB = LBound(arr)
UB = UBound(arr)

'to correct for 0-based arrays
'-----------------------------
lCol1 = lCol1 + (LB - 1)
lCol2 = lCol2 + (LB - 1)

For i = LB To UB
If arr(i, lCol1) = lLookupValue Then
ArrayLookup = arr(i, lCol2)
If bReplace Then
If ArrayLookup = strReplace Then
ArrayLookup = strReplace2
End If
End If
Exit Function
End If
Next

'for in case no match was found, pass the supplied value back
'------------------------------------------------------------
ArrayLookup = lLookupValue

End Function


RBS


"Frank & Pam Hayes" wrote in message
news:KBlKe.9141$0d.3629@trnddc02...
I have an two dimensional array (MyArray) that describes 5000 invoices.

MyArray(i, 1) is the PRODUCT
MyArray(i, 2) is the VOLUME
MyArray(i, 3) is the REVENUE
MyArray(i, 4) is the TYPE

I am using Application.Vlookup to lookup the PRODUCT in a named range on a
worksheet to see if this is a product I sold in the previous period. If
the Vlookup is successful, I put "Existing Product" in MyArray(i, 4). If
the vlookup returns an error (using FALSE as the last argument), then I
put "New Product" in MyArray(i, 4).

The Application.Vlookup is a bit slow. Can someone point me to a VBA
example where the author wrote the target range into a second Array and
then used a For Next loop to perform the same basic logic as the vlookup
would?

Thanks,

Frank Hayes




 
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
Trying to improve efficiency of array formula Anthony[_4_] Excel Worksheet Functions 8 November 25th 08 10:23 AM
Improve speed of saving a large Excel file Elmer Excel Discussion (Misc queries) 3 February 5th 07 07:15 AM
Speed up performance for very big array Cool Sport Excel Programming 10 December 15th 04 09:53 AM
How can I Improve query speed? John[_60_] Excel Programming 5 October 12th 04 01:00 PM
Howto Improve speed? Andy Excel Programming 0 January 19th 04 04:39 PM


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

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"