You can combine the IF function and VLOOKUP in Excel to make VLOOKUP return a custom result when it finds a match (or doesn’t), or to choose between two different lookup tables based on a condition. The core pattern is =IF(condition, VLOOKUP(...), alternative) or =IFERROR(VLOOKUP(...), "Not Found") for handling missing values.
What Each Function Does Alone
Before combining them, it helps to recall their separate roles. VLOOKUP searches for a value in the first column of a table and returns a value from a column to the right. Its syntax is =VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup]). The fourth argument is optional: FALSE for an exact match, TRUE or omitted for an approximate match (requires sorted data).
IF tests a condition and returns one value if true, another if false. Its syntax is =IF(logical_test, value_if_true, value_if_false). You can nest IFs, but modern Excel also offers IFS for multiple conditions.
Why Combine IF and VLOOKUP?
VLOOKUP alone has two common limitations that IF addresses:
- Error handling: When VLOOKUP finds no match, it returns
#N/A. Wrapping it inIFERROR(which is a special case of IF) lets you show a friendly message like “Not in list”. - Conditional lookup: You might need to pull from one table if a cell says “East” and another if it says “West”. An IF can pick which table to use.
- Custom output: You may want to return a label like “High” or “Low” based on the looked-up value, rather than the raw number.
Basic Example: IF with VLOOKUP for a Custom Label
Assume you have a product table with names in A2:A10 and prices in B2:B10. In D2 you type a product name. To return “Expensive” if the price is over $100, and “Cheap” otherwise, use:
=IF(VLOOKUP(D2, A2:B10, 2, FALSE) > 100, "Expensive", "Cheap")
If D2 contains “Widget” and its price is 150, the result is “Expensive”. If the price is 50, it returns “Cheap”.
Handling #N/A Errors with IFERROR
The most frequent real-world use is to avoid ugly #N/A errors. Instead of nesting IF inside VLOOKUP, you use IFERROR which is simpler:
=IFERROR(VLOOKUP(D2, A2:B10, 2, FALSE), "Not Found")
With this, if D2 has a product not in A2:A10, the cell shows “Not Found” instead of #N/A. Note: IFERROR catches any error, not just #N/A. If you only want to catch #N/A, use IF(ISNA(VLOOKUP(...)), "Not Found", VLOOKUP(...)) but that repeats the VLOOKUP, so IFERROR is usually better.
Choosing Between Two Tables with IF
Suppose you have two price tables: one for retail (A2:B10) and one for wholesale (D2:E10). In G2 you type a product name, and in H2 you type “Retail” or “Wholesale”. To get the correct price:
=IF(H2="Retail", VLOOKUP(G2, A2:B10, 2, FALSE), VLOOKUP(G2, D2:E10, 2, FALSE))
If H2 is “Retail”, it looks in the first table; otherwise it looks in the second. This works for two options. For more than two, consider nested IFs or better, the IFS function or a CHOOSE with a match.
Nested IF with VLOOKUP for Multiple Conditions
You can nest IFs to handle several cases. For example, if you want to assign a category based on a looked-up quantity:
=IF(VLOOKUP(D2, A2:B10, 2, FALSE) >= 1000, "High", IF(VLOOKUP(D2, A2:B10, 2, FALSE) >= 500, "Medium", "Low"))
This repeats the VLOOKUP three times, which is inefficient. A cleaner approach is to store the VLOOKUP result in a helper cell (e.g., E2) and then use =IF(E2>=1000, "High", IF(E2>=500, "Medium", "Low")). Or use the LET function in Excel 365 to define a variable:
=LET(price, VLOOKUP(D2, A2:B10, 2, FALSE), IF(price>=1000, "High", IF(price>=500, "Medium", "Low")))
LET requires Microsoft 365 or Excel 2024. It avoids repeating the lookup and makes the formula easier to read.
Common Mistakes and How to Avoid Them
- Using TRUE instead of FALSE: If your table isn’t sorted, an approximate match can return wrong results. Always use
FALSEfor exact matches unless you truly need approximate. - Column index off by one: Remember VLOOKUP counts from the first column of the table array, not from column A of the sheet. If your table starts at column B, the first column is 1.
- Mixing data types: If the lookup value is a number stored as text, VLOOKUP won’t match. Use
VALUE()orTEXT()to convert. - Forgetting to lock the table range: When copying formulas down, use absolute references like
$A$2:$B$10to prevent the range from shifting. - Over-nesting IFs: Excel allows up to 64 nested IFs, but readability suffers. Use
IFSorSWITCHfor many conditions.
Alternatives to VLOOKUP with IF
Excel 365 and Excel 2021 offer XLOOKUP, which is more flexible and often eliminates the need for IF in error handling because it has built-in if_not_found argument. For example:
=XLOOKUP(D2, A2:A10, B2:B10, "Not Found")
This returns “Not Found” if no match. XLOOKUP also allows lookup to the left, which VLOOKUP cannot. For conditional table selection, you can still use IF with XLOOKUP, but the syntax is cleaner.
Another alternative is INDEX and MATCH, which are more powerful for left lookups and non-adjacent columns. Combined with IF, you can do complex lookups without the column index limitation.
Frequently Asked Questions
Can I use IF inside VLOOKUP? Yes, you can put an IF as the lookup_value or as the table_array, but it’s usually more practical to put VLOOKUP inside IF. For example, =VLOOKUP(IF(A1="Yes", "A", "B"), table, 2, FALSE) works.
Why does my combined IF and VLOOKUP return #N/A even with IFERROR? If you wrote =IFERROR(VLOOKUP(...), "Not Found") and still see #N/A, check that the formula is not missing the IFERROR wrapper. Also, if the error is not from VLOOKUP but from another part of the formula (like a division by zero), IFERROR will catch it too, but if you used IF(ISNA(...)), it only catches #N/A.
What is the difference between IFERROR and IF(ISNA())? IFERROR catches all errors (#N/A, #VALUE!, #DIV/0!, etc.), while IF(ISNA()) only catches #N/A. If you want to show a custom message only for missing lookups but let other errors surface, use IF(ISNA()).
How do I use IF with VLOOKUP to return a blank cell? Use =IFERROR(VLOOKUP(...), "") to return an empty string. That makes the cell look blank but it still contains a formula. To truly leave it empty, you’d need a macro, but the empty string is usually fine.
Does the order of arguments in IF matter? Yes. The first argument is the condition, second is the value if true, third is the value if false. If you omit the third argument, IF returns FALSE when the condition is false, which can be confusing.
Can I combine IF and VLOOKUP across sheets? Yes, just reference the sheet name in the table array, e.g., =IFERROR(VLOOKUP(D2, Sheet2!$A$2:$B$10, 2, FALSE), "Not Found"). Ensure the sheet name is spelled correctly and, if it contains spaces, enclose it in single quotes like 'My Sheet'!.
Related
- Redo Shortcut in Windows: Ctrl+Y and More
- Best Online Data Entry Sites That Actually Pay
- Google Sheets Query Multiple Criteria: How-To
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Excel CONCAT Examples: Formulas for Joining TextTo join a first name in A2 and a surname in B2 with a space between them, enter…
- PHP String Functions: How to Work With Text SafelyPHP string functions let you inspect, search, change, split, join, and format text. Common choices include strlen() for…
- JavaScript Function Return String: How It WorksYes, a JavaScript function can return a string by using the return statement followed by a string value,…
- Interpolate Between Two Points in Excel With a FormulaTo interpolate between two points in Excel, use a straight-line formula: estimated y = y1 + (target x…
- Return Weekday Dates in Excel: WORKDAY and ExamplesTo return the next weekday date in Excel, use the WORKDAY function. If A2 contains Friday, January 9,…
- Excel Formula for Numbering Rows: Static and DynamicTo number rows in Excel, enter =ROW()-1 in cell A2 when row 1 contains a header, then fill…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.