+ Reply to Thread
Results 1 to 16 of 16

VBA Arrays - Faster alternative than Vlookup?

  1. #1
    Registered User
    Join Date
    03-23-2011
    Location
    los angeles, ca
    MS-Off Ver
    Excel 2007
    Posts
    8

    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
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Arrays - Faster alternative than Vlookup?

    Your example above puts the array data into computer memory and the VLOOKUP is applied to that, I can't imagine anything being faster than that.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Arrays - Faster alternative than Vlookup?

    Worksheet function calculates faster in the range.
    If you explain more about what you are really trying to do, there may be a faster way.
    e.g
    Please Login or Register  to view this content.
    Last edited by jindon; 08-21-2012 at 08:43 PM. Reason: Row size must be less than 65536 to calculate in array

  4. #4
    Registered User
    Join Date
    03-23-2011
    Location
    los angeles, ca
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Arrays - Faster alternative than Vlookup?

    Thank you for the replies.

    I am trying to value a set of securities using Monte Carlo simulations. I have 50 securities to value on 50 different days. Each security contains 12 parts, which are valued separately and added together. The calculation for part 1 is slightly different than parts 2 through 12, which is contained in a separate module and called when necessary. There are 3 vlookups for part 1 which are executed 3x50x50x12 times and 3 for parts 2-12 which are executed 3x50x50x12x(#simulations) times.

    I need to run 50,000 simulations, but as of now even 10 simulations takes 30 seconds. When I replace the values derived from the vlookups with made-up constants, 10 simulations only takes 2 seconds.

  5. #5
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: VBA Arrays - Faster alternative than Vlookup?

    I wonder if:

    1. VBA collections will be faster?
    2. VLOOKUP using sorted array and 'True' as the last parameter. I think this makes binary lookup for vlookup possible. You can search web for shallsort algorithms if you decide to go this route.
    Regards,
    Vandan

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Arrays - Faster alternative than Vlookup?

    I'm not sure if I can help you yet, but can you upload a small sample file with your desired results?

  7. #7
    Registered User
    Join Date
    03-23-2011
    Location
    los angeles, ca
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Arrays - Faster alternative than Vlookup?

    Sorry I'm brand new to this forum and don't know how to upload files.

    I'm also unfamiliar with collections. Is there any easy way to assign a range (e.g. Data!A1:F10000) to a collection?

    Thanks to all for the assistance.

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Arrays - Faster alternative than Vlookup?

    If go down the page, you will find "Go Advanced" button.

    Then "Manage Attachment" will leads you to select the files and attach.

    Regarding Collection, I prefer to use Dictionary object, so that you can refer afterwards.

  9. #9
    Registered User
    Join Date
    03-23-2011
    Location
    los angeles, ca
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Arrays - Faster alternative than Vlookup?

    I am going to private message you with the file if that's OK.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Arrays - Faster alternative than Vlookup?

    Quote Originally Posted by danmcdon View Post
    I am going to private message you with the file if that's OK.
    I don't think so, it against the rule here.

    If your data have sensitive data, just change them to dummy.
    Is that possible?

  11. #11
    Registered User
    Join Date
    03-23-2011
    Location
    los angeles, ca
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Arrays - Faster alternative than Vlookup?

    I cut the results I'm seeking down to 5 securites and 5 days and did not include all the input data but hopefully this will ggive you the idea.
    Attached Files Attached Files

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Arrays - Faster alternative than Vlookup?

    Hummm

    What are the result that you are expecting?
    Can you attach the result and the calculation algorithm?

  13. #13
    Registered User
    Join Date
    03-23-2011
    Location
    los angeles, ca
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Arrays - Faster alternative than Vlookup?

    The results in this abbreviated case on sheet "Results" would be -$18,875,291, -$18,881,589, -$18,889,169, -$18,906,998, -$18,911,267 in cells B3:B7, and $0 for the remaining values. I'm not sure what you mean by calculation algorithm other than the code in the file above.

    Do you notice any obvious inefficiencies or suggestions for how to look up values such as those in sheet "FINAL_BRAZIL." In the real file there are 3 additional sheets like "FINAL_BRAZIL" with input data, each containing over 50,000 observations.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA Arrays - Faster alternative than Vlookup?

    Sorry, but I don't want to read all through your code to find out what you are trying to do.

    Hope someone else will come to help you.

  15. #15
    Registered User
    Join Date
    03-23-2011
    Location
    los angeles, ca
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Arrays - Faster alternative than Vlookup?

    Thanks for trying.

  16. #16
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: VBA Arrays - Faster alternative than Vlookup?

    @danmcdon: your code errors out for me at several steps due to defined names issues. So I can be very general in help.
    Snap3.jpg

    1. Seems like most of your VLOOKUPs are happening on the ranges on the worksheet rather than your original post
    Please Login or Register  to view this content.
    If you changed your VLOOKUPs to above method...it "should" be faster.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1