How to use the HLOOKUP function
The HLOOKUP function lets you search the top row in a data range for a value and return another value on the same column in a row you specify.
HLOOKUP stands for horizontal lookup and your data (table_array) must be organized into records, a record on each row.
Table of Contents
1. Overview
The HLOOKUP function in cell E2 searches the top row of cell range B5:E14 for the value "Item" and it is found in the second column.
Formula in cell E2:
The third argument tells the function which row to fetch the value from, in this case, it is four.
The intersection of the second column and the fourth row is value DD and that is the returning value in cell E2.
5 easy ways to VLOOKUP and return multiple values
I recommend the FILTER function if you are an Excel 365 user.
2. Syntax
HLOOKUP(lookup_value, table_array, row_index_num, [range_lookup])
3. Arguments
Lookup_value | Required. The first argument is the lookup value. This is the value you want to find in the top row of the table array. |
Table_array | Required. This is a cell range or an array that you want to search. |
Row_index_num | Required. Here you specify which row you want to fetch the value from. |
Range_lookup | Optional. TRUE tells the function to do an approximate match and this is the default value.
FALSE - Exact match |
Make sure the top row in the table array is sorted in ascending order. FALSE means exact match and this mode is probably what you will use the most. Take note that if you use the wrong argument here you may get incorrect results.
4. HLOOKUP video
https://www.youtube.com/watch?v=5I--h_l0t7U
5. Approximate match
The HLOOKUP function lets you search using an approximate match if you enter TRUE in the fourth argument or don't specify anything at all. In the example above there are four different ranges, 0 and less than 100, 100 and less than 250, 250 and less than 500, and 500 and more.
The approximate match setting finds the largest value that is less than or equal to the lookup_value.
The value 189 in cell C2 can't be found in the top row of cell range C5:F6 so the largest value that is less than lookup_value (189) is matched, in this case, 100. The corresponding value is 12.1 and that value is returned in cell C3.
You want, in most cases, to use the EXACT match, however, the default value is an APPROXIMATE match. So make sure you know what you are doing.
True - Approximate match (default)
False - Exact match
This means that you get the approximate match if you don't specify the fourth argument at all. It is very important that the top row is sorted in ascending order if the fourth argument is left out or is True (approximate match).
6. Horizontal and vertical lookup
The following formula lets you search both horizontally and vertically. The formula in cell E2 looks for Item in the top horizontal row and 141 in column "Invoice", the intersecting cell is C9 so the formula returns AA.
This example demonstrates a formula that combines both the MATCH and HLOOKUP functions to perform a horizontal and vertical lookup.
Formula in cell E2:
Explaining formula in cell E2
Step 1 - Find the relative position
The MATCH function returns the relative position of an item in an array or cell reference that matches a specified value in a specific order.
MATCH(lookup_value, lookup_array, [match_type])
MATCH(C3, B5:B14, 0)
The MATCH function returns a number representing the row of which a given value is found.
MATCH(C3, B5:B14, 0)
becomes
MATCH(141, {"Invoice"; 190; 131; 181; 141; 165; 182; 183; 177; 126}, 0)
and returns 5. Number 141 is the fifth position in B5:B14.
Step 2 - Find value and return corresponding value based on the row number
HLOOKUP(C2, B5:E14, MATCH(C3, B5:B14, 0), FALSE)
becomes
HLOOKUP(C2, B5:E14, 5, FALSE)
and returns "AA".
7. Horizontal and vertical lookup - INDEX + MATCH
You can also use INDEX and MATCH functions to get a value in a cross-reference table. The formula below works just like the formula above in section 6.
Formula in cell E2:
This formula is actually easier to understand and remember than the HLOOKUP + MATCH functions, this is the method I prefer.
Explaining formula in cell E2
Step 1 - Find the relative position of the column header name
The MATCH function returns the relative position of an item in an array or cell reference that matches a specified value in a specific order.
MATCH(lookup_value, lookup_array, [match_type])
MATCH(C2,B5:E5,0)
becomes
MATCH("Amount",{"Invoice","Item","Size","Amount"},0)
and returns 4. "Amount" is the fourth value in the array.
Step 2 - Find the relative position vertically
MATCH(C3, B6:B14, 0)
becomes
MATCH(131,{190; 131; 181; 141; 165; 182; 183; 177; 126},0)
and returns 2. 131 is the second value in the array.
Step 3 - Get value based on row and column number
The INDEX function returns a value based on a row and column number.
INDEX(array, [row_num], [column_num])
INDEX(B6:E14, MATCH(C3, B6:B14, 0), MATCH(C2, B5:E5, 0))
becomes
INDEX(B6:E14, 2, 4)
and returns 155.09 in cell E2.
8. HLOOKUP - multiple tables
The formula below uses a text string specified in a cell to search a given Excel Table using a lookup value.
There are two Excel Tables in the image above, Table50 and Table51. Column G contains which Excel Table to searc, column H which column to search, and finally column I which row.
Formula in cell J4:
Explaining formula in cell J4
Step 1 - Convert text string to a structured reference
The INDIRECT function lets you convert a cell value to a cell reference or in this case a structured reference.
The INDIRECT function is volatile, so use it with care. Volatile means it is recalculated every time the worksheet is recalculated. If you have many INDIRECT functions, they may slow down your workbook considerably.
INDIRECT(G4)
becomes
INDIRECT("Table50[#All]")
and returns Table50[#All].
Step 2 - Get value from cell range
HLOOKUP(H4, INDIRECT(G4), I4, FALSE)
becomes
HLOOKUP("Size", Table50[#All], 6, FALSE)
and returns "Large" in cell J4.
9. HLOOKUP - VBA Example
The following macro lets you search a cell range using the parameters specified in cells C2 and C3. A messagebox appears and tells you the found value.
VBA Code
Sub HLP() MsgBox Application.WorksheetFunction.HLookup(Range("C2"), Range("B5:E14"), Range("C3"), False) End Sub
10. Function not working
The HLOOKUP function returns
- #N/A error if the lookup_value is not found.
- #NAME? error if you misspell the function name.
- propagates errors, meaning that if the input contains an error (e.g., #VALUE!, #REF!), the function will return the same error.
10.1 Troubleshooting the error value
When you encounter an error value in a cell a warning symbol appears, displayed in the image above. Press with mouse on it to see a pop-up menu that lets you get more information about the error.
- The first line describes the error if you press with left mouse button on it.
- The second line opens a pane that explains the error in greater detail.
- The third line takes you to the "Evaluate Formula" tool, a dialog box appears allowing you to examine the formula in greater detail.
- This line lets you ignore the error value meaning the warning icon disappears, however, the error is still in the cell.
- The fifth line lets you edit the formula in the Formula bar.
- The sixth line opens the Excel settings so you can adjust the Error Checking Options.
Here are a few of the most common Excel errors you may encounter.
#NULL error - This error occurs most often if you by mistake use a space character in a formula where it shouldn't be. Excel interprets a space character as an intersection operator. If the ranges don't intersect an #NULL error is returned. The #NULL! error occurs when a formula attempts to calculate the intersection of two ranges that do not actually intersect. This can happen when the wrong range operator is used in the formula, or when the intersection operator (represented by a space character) is used between two ranges that do not overlap. To fix this error double check that the ranges referenced in the formula that use the intersection operator actually have cells in common.
#SPILL error - The #SPILL! error occurs only in version Excel 365 and is caused by a dynamic array being to large, meaning there are cells below and/or to the right that are not empty. This prevents the dynamic array formula expanding into new empty cells.
#DIV/0 error - This error happens if you try to divide a number by 0 (zero) or a value that equates to zero which is not possible mathematically.
#VALUE error - The #VALUE error occurs when a formula has a value that is of the wrong data type. Such as text where a number is expected or when dates are evaluated as text.
#REF error - The #REF error happens when a cell reference is invalid. This can happen if a cell is deleted that is referenced by a formula.
#NAME error - The #NAME error happens if you misspelled a function or a named range.
#NUM error - The #NUM error shows up when you try to use invalid numeric values in formulas, like square root of a negative number.
#N/A error - The #N/A error happens when a value is not available for a formula or found in a given cell range, for example in the VLOOKUP or MATCH functions.
#GETTING_DATA error - The #GETTING_DATA error shows while external sources are loading, this can indicate a delay in fetching the data or that the external source is unavailable right now.
10.2 The formula returns an unexpected value
To understand why a formula returns an unexpected value we need to examine the calculations steps in detail. Luckily, Excel has a tool that is really handy in these situations. Here is how to troubleshoot a formula:
- Select the cell containing the formula you want to examine in detail.
- Go to tab “Formulas” on the ribbon.
- Press with left mouse button on "Evaluate Formula" button. A dialog box appears.
The formula appears in a white field inside the dialog box. Underlined expressions are calculations being processed in the next step. The italicized expression is the most recent result. The buttons at the bottom of the dialog box allows you to evaluate the formula in smaller calculations which you control. - Press with left mouse button on the "Evaluate" button located at the bottom of the dialog box to process the underlined expression.
- Repeat pressing the "Evaluate" button until you have seen all calculations step by step. This allows you to examine the formula in greater detail and hopefully find the culprit.
- Press "Close" button to dismiss the dialog box.
There is also another way to debug formulas using the function key F9. F9 is especially useful if you have a feeling that a specific part of the formula is the issue, this makes it faster than the "Evaluate Formula" tool since you don't need to go through all calculations to find the issue..
- Enter Edit mode: Double-press with left mouse button on the cell or press F2 to enter Edit mode for the formula.
- Select part of the formula: Highlight the specific part of the formula you want to evaluate. You can select and evaluate any part of the formula that could work as a standalone formula.
- Press F9: This will calculate and display the result of just that selected portion.
- Evaluate step-by-step: You can select and evaluate different parts of the formula to see intermediate results.
- Check for errors: This allows you to pinpoint which part of a complex formula may be causing an error.
The image above shows cell reference C2 converted to hard-coded value using the F9 key. The HLOOKUP function needs the lookup_value in one of the top cells in the datatable B5:E5 which is not the case in this example. We have found what is wrong with the formula.
Tips!
- View actual values: Selecting a cell reference and pressing F9 will show the actual values in those cells.
- Exit safely: Press Esc to exit Edit mode without changing the formula. Don't press Enter, as that would replace the formula part with the calculated value.
- Full recalculation: Pressing F9 outside of Edit mode will recalculate all formulas in the workbook.
Remember to be careful not to accidentally overwrite parts of your formula when using F9. Always exit with Esc rather than Enter to preserve the original formula. However, if you make a mistake overwriting the formula it is not the end of the world. You can “undo” the action by pressing keyboard shortcut keys CTRL + z or pressing the “Undo” button
10.3 Other errors
Floating-point arithmetic may give inaccurate results in Excel - Article
Floating-point errors are usually very small, often beyond the 15th decimal place, and in most cases don't affect calculations significantly.
Useful resources
HLOOKUP function - Microsoft
HLOOKUP function
Functions in 'Lookup and reference' category
The HLOOKUP function function is one of 25 functions in the 'Lookup and reference' category.
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form