+ Reply to Thread
Results 1 to 18 of 18

Using arrays with application.vlookup still taking time

  1. #1
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    15

    Using arrays with application.vlookup still taking time

    Hi All

    I'm new to the forum and hoping to get some help please.

    I've got the following code in VBA which I thought would save me alot of time when doing vlookups as it is all being stored in an array. But it still takes A LONG time to run through. Am I doing something wrong here?

    Please Login or Register  to view this content.
    Thanks
    Last edited by Ranmandaman; 06-12-2013 at 06:54 AM. Reason: Added code tags - sorry!

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: Using arrays with application.vlookup still taking time

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

  3. #3
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Using arrays with application.vlookup still taking time

    Hi Ranmandaman,
    try it
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Using arrays with application.vlookup still taking time

    Hi Nilem

    That's brilliant. It speeds up the lookup so much.

    Why did my original array lookup run so slow? I would have thought looking up array in memory would be much faster. It was slower then doing a vlookuo formula on the sheet itself!

    Also I notice that using the scripting dictionary not all of the correct values from the data array are bein returned. Like it will just return the lookup value rather than data(i,3). I tried to research more into scripting to see if I could fix but sadly haven't been able to. Could you help out please!

    Thanks!

  5. #5
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Using arrays with application.vlookup still taking time

    You are right, the arrays are fast. But using worksheet function in a loop is very slow code execution.
    Could you show an example file where the code produces incorrect results.

  6. #6
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Using arrays with application.vlookup still taking time

    Hi Nilem. Sorry for late reply. It's a bit difficult to send an example without spending a while changing all the confidential data. But I can explain how the file works. The data array is basically 3 columns. A is the counterparty name, B is the subparent and C is parent. The dictionary script looks up all the items in lookup array against data array and should take the Parent. I notice that for many of the items it still keeps the lookup item (ie the counterparty name) rather then the parent. Each counterparty in the data array is different but there are duplicated parents as you would imagine

    Does this help?

  7. #7
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Using arrays with application.vlookup still taking time

    Hi Ranmandaman
    If you want the code to run as the Vlookup (or as your code in the first post), uncomment ': .Remove Data(i, 1). That is, must be so
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Using arrays with application.vlookup still taking time

    I've found out why it happens. The lookup array contains duplicated items (as expected). So when the

    Please Login or Register  to view this content.
    is set it only is set to the last one and when
    Please Login or Register  to view this content.
    is set it only will be set for the last row for the duplicated value. Any advice on how to fix this?

  9. #9
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Using arrays with application.vlookup still taking time

    Quote Originally Posted by nilem View Post
    Hi Ranmandaman
    If you want the code to run as the Vlookup (or as your code in the first post), uncomment ': .Remove Data(i, 1). That is, must be so
    Please Login or Register  to view this content.
    Yep already removed the comment. Issue is due to the above (I think!). Any ideas how to overcome?

  10. #10
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Using arrays with application.vlookup still taking time

    Ranmandaman, here can be lot of options. Try to jot down a small example file (replace your data on, for example, apples, bananas, etc.), and show that you want to get.

  11. #11
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Using arrays with application.vlookup still taking time

    Hi

    Sorry for the long delay between replies. I've created a small file to give an example of the issue.

    The attached example contains a mapping table in cells A1:C7

    I want to lookup each item in cells D1:D7 against A1:A7 and return the corresponding item in column C to column E. As you can see in column D I have duplicate items and using the dictionary method only the last duplicated item is correctly mapping.

    I would like to see it give the result as below

    Apple Fruit
    Pizza Food
    Apple Fruit
    Mango Fruit
    Pizza Food
    Apple Fruit
    Mango Fruit


    Hope this helps to explain the problem!
    Thanks
    Attached Files Attached Files

  12. #12
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Using arrays with application.vlookup still taking time

    Hi Ranmandaman,
    try
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Using arrays with application.vlookup still taking time

    Thats excellent!! It seems so simple now that youve shown me but I couldnt figure out how to do it lol

    Will try that code against my main data but it worked a charm on the example.

    Thanks Nilem!

  14. #14
    Registered User
    Join Date
    11-21-2009
    Location
    Coburn, Pennsylvania
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Using arrays with application.vlookup still taking time

    I suggest you add the following line to your code before any other code lines.
    This will turn off screen updating.
    Application.ScreenUpdating = False

    and then add
    Application.ScreenUpdating = True

    right before your "End SUB" line

  15. #15
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Using arrays with application.vlookup still taking time

    Already done! Thanks!

  16. #16
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Using arrays with application.vlookup still taking time

    Sorry to come back to this! I've had to change the Iif formula to a regular If because it was adding items to the dictionary after it couldn't find it the first time. And then it would return a empty value against it. Very strange - would you know why that is?

  17. #17
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Using arrays with application.vlookup still taking time

    Hi Ranmandaman,
    could you give an example where the code is not working correctly

  18. #18
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Using arrays with application.vlookup still taking time

    Hi Nilem - not to worry I just changed the Iif to a normal If statement. I have another question however. For some reason the loading of the array into the dictionary is
    Really slow for some of the files I use the code in but not on others. The files are all very similar and the usual application.screenupdating is false etc

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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