Consulting

Results 1 to 6 of 6

Thread: VBA Arrays - Faster alternative than Vlookup?

  1. #1
    VBAX Newbie
    Joined
    Aug 2012
    Posts
    2
    Location

    VBA Arrays - Faster alternative than Vlookup?

    Hi! I have several multi-dimensional arrays from which I am using different lookup values to pull data. I'm currently using WorksheetFunction.Vlookup which I think is going very slow. I have many iterations and several different arrays so the total impact in run time is pretty significant. What is the best way to lookup values in an array? Or any other strategies to speed things up? THANKS!!

    ------------------

    'example

    Dim ArrData

    ArrData = range("data")

    Value = Application.WorksheetFunction.Vlookup(LookupValue, ArrData, 5, 0)

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    I don't really have a answer for you, just opinions.

    1. Unless you have other needs from your array's, I would skip them completely and use Range.Find.

    2. Using brute force may be faster than Vlookup.

    3. You can use binary search.

    David


  3. #3
    VBAX Newbie
    Joined
    Aug 2012
    Posts
    2
    Location
    Thanks for your opinions

    How would Range.Find work with a multidimensional array? What does it return? I tried:

    pos = Range("data").Find(LookupValue, LookIn:=xlValues, LookAt=xlWhole)

    The result was a type mismatch error

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by danmcdon
    How would Range.Find work with a multidimensional array? What does it return? I tried:

    pos = Range("data").Find(LookupValue, LookIn:=xlValues, LookAt=xlWhole)

    The result was a type mismatch error
    If you Dimmed pos, Dim it as Range and use:
    [vba]set pos = Range("data").Find(LookupValue, LookIn:=xlValues, LookAt:=xlWhole)[/vba] (note the added missing colon too), or if you're only searching in column 1 of the range then
    [vba]set pos = Range("data").columns(1).Find(LookupValue, LookIn:=xlValues, LookAt:=xlWhole)[/vba] .Find returns a range object, so if not found pos will be nothing, so use the likes of:[vba]if not pos is nothing then myValue = pos.offset(,4).value[/vba] or you can use the .row property of pos to return the absolute row on the sheet.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Have you tried sorting the data before running VLookup?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    this will do:
    [vba]
    on error resume next
    msgbox Range("data").Find(LookupValue, ,xlValues, 1).address
    [/vba]

    To look in the third column of an array and returning the corresponding value in column 5:

    [vba]
    Sub snb_002()
    sn = Cells(1).CurrentRegion
    c01 = sn(Application.Match("cc7", Application.Index(sn, , 3),0), 5)
    End Sub
    [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •