+ Reply to Thread
Results 1 to 12 of 12

How can i translate an Index/Match formula into VBA code?

  1. #1
    Registered User
    Join Date
    08-21-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    27

    How can i translate an Index/Match formula into VBA code?

    Hi,
    I've been using the following formula in a macro for some time and it has been working as expected, however with large data sets it becomes extremely slow and oftentimes will crash. So i'm trying to figure how I might be able to convert this formula into VBA.

    Please Login or Register  to view this content.
    Any help would be really appreciated.

    Thanks

  2. #2
    Forum Contributor Obsessed's Avatar
    Join Date
    05-22-2013
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 365
    Posts
    215

    Re: How can i translate an Index/Match formula into VBA code?

    How have you been using it in a macro? As a formula the macro is writing? Share your code or workbook with us...it will help us make suggestions as to how to improve the speed. Simply converting this to a macro executable form won't speed this up...in fact it will slow it down. VBA is single threaded vs. Excel Formulas calculating multi-threaded.
    Want to show appreciation for the help you received from a member? Give them reps by clicking the bottom left of their post!

  3. #3
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: How can i translate an Index/Match formula into VBA code?

    Agree with @Obsessed, but I did this just for fun. If you put this code in a Module, then you can call it like any other function by typing this into any cell:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  4. #4
    Registered User
    Join Date
    08-21-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    27

    Re: How can i translate an Index/Match formula into VBA code?

    Thanks

    Please Login or Register  to view this content.
    That's the code I run in the macro but it is really just the code of an excel formula added to the macro and I think this is the reason it is so slow. If I can have it written in VBA I think it may improve the speed?

    For example I saw a variation of the code below online and tried adjusting it to work with my formula but I can't seem to execute it properly.

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor Obsessed's Avatar
    Join Date
    05-22-2013
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 365
    Posts
    215

    Re: How can i translate an Index/Match formula into VBA code?

    The first bit of code is faster actually. The first snippet shows you writing an Index Match within Excel and using Excel to calculate it, vs having the VBA engine calculate it, which is the second snippet. Do you have calculation set to manual? When I have a ton of these (I literally have one workbook that I use at work that has 4 million index matches and sumifs...don't ask.), I typically systematically write a few hundred thousand, calculate them (with Excel's engine, not VBA's), and then hardcode them, and then proceed. I can typically calculate 4 million of these formulas within 7 or 8 minutes on my machine doing it this way, and the indexed arrays are 100k+ lines.
    Last edited by Obsessed; 11-06-2015 at 10:46 AM.

  6. #6
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: How can i translate an Index/Match formula into VBA code?

    You might be able to speed it up by writing the formula to the entire range, instead of one cell and copying via autofill?


    I may be interpreting that wrong, but where are you actually writing the formula? One cell (G2?) or many cells?



    EDIT:
    I think you might want something like this:

    Please Login or Register  to view this content.
    That's one of the awesome shortcuts that using the .FormulaR1C1 offers - you can write formulas to an enormous range of cells in one line of code.
    Last edited by GeneralDisarray; 11-06-2015 at 10:47 AM.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  7. #7
    Forum Contributor Obsessed's Avatar
    Join Date
    05-22-2013
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 365
    Posts
    215

    Re: How can i translate an Index/Match formula into VBA code?

    Quote Originally Posted by GeneralDisarray View Post
    You might be able to speed it up by writing the formula to the entire range, instead of one cell and copying via autofill?


    I may be interpreting that wrong, but where are you actually writing the formula? One cell (G2?) or many cells?
    I'd expect that it's not writing the formulas that are taking forever, but rather calculating them. Excel can write 100k+ formulas in the matter of seconds even on the slowest of machines. Calculating them is another story.

  8. #8
    Registered User
    Join Date
    08-21-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    27

    Re: How can i translate an Index/Match formula into VBA code?

    Quote Originally Posted by Obsessed View Post
    The first bit of code is faster actually. The first snippet shows you writing an Index Match within Excel and using Excel to calculate it, vs having the VBA engine calculate it, which is the second snippet. Do you have calculation set to manual? When I have a ton of these (I literally have one workbook that I use at work that has 4 million index matches and sumifs...don't ask.), I typically systematically write a few hundred thousand, calculate them (with Excel's engine, not VBA's), and then hardcode them, and then proceed. I can typically calculate 4 million of these formulas within 7 or 8 minutes on my machine doing it this way, and the indexed arrays are 100k+ lines.
    I'm not sure if i have the calculation set to manual or not.... how do I check? What I do is have the macro write that formula above (the first one) into a cell and then autofill all the way down to however many rows there are. This is where it becomes extremely slow. I have a about 5 or 6 similar formulas in the same macro so when they all run and autofill it can sometimes crash.


    Agree with @Obsessed, but I did this just for fun. If you put this code in a Module, then you can call it like any other function by typing this into any cell:
    @walruseggman

    thanks for this... i tried it but it only returns zero in the cell?

  9. #9
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: How can i translate an Index/Match formula into VBA code?

    As obsessed mentioned, please post an example worksheet, and also your expected/desired results.

  10. #10
    Forum Contributor Obsessed's Avatar
    Join Date
    05-22-2013
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 365
    Posts
    215

    Re: How can i translate an Index/Match formula into VBA code?

    Share your workbook with us if you can. I'm betting the reason it is slow is you have auto calculation on, and your index matches are volatile. Means every time you write a new formula to calculate, it recalculates all of them, making the sheet exponentially slower.
    Last edited by Obsessed; 11-06-2015 at 11:01 AM.

  11. #11
    Registered User
    Join Date
    08-21-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    27

    Re: How can i translate an Index/Match formula into VBA code?

    Ok, I've attached a sample worksheet.

    The formula for this worksheet (which would go in column I) would be as follows:
    Please Login or Register  to view this content.
    So I want to match on ID and Event and the word "ACK Received" and I want the value returned to be the date in sheet1.

    The problem is I need to have three variations of this formula, one where the word is "Reprocessed" one with "NACK Received" and another with "Accepted". And the larger the dataset is the slower it becomes. I have over 1,000 row in the ones I'm working with at the moment and it basically just crashes.

    @Obsessed
    I think I have auto calculation on but how do i ensure the formula is executed if i turn it off?
    Attached Files Attached Files

  12. #12
    Forum Contributor Obsessed's Avatar
    Join Date
    05-22-2013
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 365
    Posts
    215

    Re: How can i translate an Index/Match formula into VBA code?

    Can you share the complete code as well? I'd like to see how you have it setup to see if it can be improved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Translate formula to VBA code please :)
    By galvinpaddy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-06-2015, 06:14 AM
  2. [SOLVED] Translate array formula to VBA code
    By Villalobos in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-03-2015, 04:49 PM
  3. Index/Match formula into VBA code
    By Justmegan93 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-06-2015, 05:18 AM
  4. Translate Formula to VBA Code
    By mkeys4 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-17-2013, 12:44 PM
  5. Replies: 3
    Last Post: 05-02-2013, 01:31 AM
  6. comparision between two excel sheet using VBA Code for Index/match/and/iferror formula
    By gaurangaero in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-28-2012, 10:08 AM
  7. translate ws formula to vba code
    By L Scholes in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-16-2006, 10:40 PM

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