Alternative lookup formulas

March 24, 2009JPNo CommentsRate This ArticlenewLinks to this article


    I was reading Dealing with VLOOKUP and GETPIVOTDATA errors by Ken Puls and noticed he wraps VLOOKUP formulas in IF statements like this:

=IF(ISNA(VLOOKUP(rngToLookup,rngToLookIn,ColToReturn,False),0, VLOOKUP(rngToLookup,rngToLookIn,ColToReturn,False))

    The way it works is first, the code does the VLOOKUP and if the result is #N/A, it returns zero, otherwise it performs the VLOOKUP (again!) and returns the result.

    The problem with this formula should be obvious: if your value exists, the VLOOKUP is performed twice! Imagine filling this formula down 10, 50, 100 columns and the resulting performance hit on your workbook.

    A few of the commenters suggested alternatives which would work better. My favorite is the COUNTIF alternative:

=IF(COUNTIF(LeftmostColumn_of_rngToLookIn,rngToLookup),VLOOKUP(rngToLookup, rngToLookIn, ColToReturn, False), 0)

    This formula uses COUNTIF to see if the value exists in the leftmost column (the column that VLOOKUP searches) and if it doesn't exist, it skips the VLOOKUP.

About JP
I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space if you want to learn more about VBA. Keep Reading »

↑ Scroll to top
Previous Post:

Next Post:

Speak Your Mind

Tell us what you're thinking...

Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].




Site last updated September 2, 2010 @ 7:03 pm