Difference between revisions of "Documentation/How Tos/Calc: HLOOKUP function"
From Apache OpenOffice Wiki
< Documentation | How Tos
(→Example:) |
(Initial content is now complete) |
||
Line 25: | Line 25: | ||
[[Image:Calc_hlookup_example.png|center]] | [[Image:Calc_hlookup_example.png|center]] | ||
− | <tt>'''HLOOKUP(3; A1: | + | <tt>'''HLOOKUP(3; A1:E2; 2; 0)'''</tt> |
: returns <tt>'''A'''</tt>. Cell A1 in the top row matches <tt>'''3'''</tt>. The top row happens to be sorted, but this is not necessary in mode <tt>'''0'''</tt>. | : returns <tt>'''A'''</tt>. Cell A1 in the top row matches <tt>'''3'''</tt>. The top row happens to be sorted, but this is not necessary in mode <tt>'''0'''</tt>. | ||
− | <tt>'''HLOOKUP(4; A1: | + | <tt>'''HLOOKUP(4; A1:E2; 2; 0)'''</tt> |
: returns <tt>'''#N/A'''</tt>. There is no match for <tt>'''4'''</tt>. | : returns <tt>'''#N/A'''</tt>. There is no match for <tt>'''4'''</tt>. | ||
− | <tt>'''HLOOKUP("CAT"; A1: | + | <tt>'''HLOOKUP("CAT"; A1:E2; 2; 0)'''</tt> |
: returns <tt>'''C'''</tt>. The difference between lower and upper case is ignored, so <tt>'''CAT'''</tt> matches <tt>'''cat'''</tt>. | : returns <tt>'''C'''</tt>. The difference between lower and upper case is ignored, so <tt>'''CAT'''</tt> matches <tt>'''cat'''</tt>. | ||
− | <tt>'''HLOOKUP("m.t"; A1: | + | <tt>'''HLOOKUP("m.t"; A1:E2; 2; 0)'''</tt> |
: returns <tt>'''D'''</tt> if regular expressions are enabled (and <tt>'''#N/A'''</tt> if not). The dot '.' stands for 'any single character' in a regular expression, so <tt>'''m.t'''</tt> matches <tt>'''mat'''</tt>. | : returns <tt>'''D'''</tt> if regular expressions are enabled (and <tt>'''#N/A'''</tt> if not). The dot '.' stands for 'any single character' in a regular expression, so <tt>'''m.t'''</tt> matches <tt>'''mat'''</tt>. | ||
− | <tt>'''HLOOKUP(".at"; A1: | + | <tt>'''HLOOKUP(".at"; A1:E2; 2; 0)'''</tt> |
: returns <tt>'''C'''</tt> if regular expressions are enabled. <tt>'''cat'''</tt> matches; so does <tt>'''mat'''</tt>, but the leftmost match is always found in mode <tt>'''0'''</tt>. | : returns <tt>'''C'''</tt> if regular expressions are enabled. <tt>'''cat'''</tt> matches; so does <tt>'''mat'''</tt>, but the leftmost match is always found in mode <tt>'''0'''</tt>. | ||
− | <tt>'''HLOOKUP("at"; A1: | + | <tt>'''HLOOKUP("at"; A1:E2; 2; 0)'''</tt> |
: returns <tt>'''#N/A'''</tt> if '<i>Search criteria + and <> must apply to whole cells</i>' is enabled and <tt>'''C'''</tt> otherwise. <tt>'''at'''</tt> does not match any whole cell contents, but it matches part of <tt>'''cat'''</tt>. | : returns <tt>'''#N/A'''</tt> if '<i>Search criteria + and <> must apply to whole cells</i>' is enabled and <tt>'''C'''</tt> otherwise. <tt>'''at'''</tt> does not match any whole cell contents, but it matches part of <tt>'''cat'''</tt>. | ||
− | <tt>'''HLOOKUP(3; A1: | + | <tt>'''HLOOKUP(3; A1:E2; 2; 1)'''</tt> |
− | :returns <tt>'''A'''</tt>. Cell A1 in the top row matches <tt>'''3'''</tt>. The top row must be sorted in mode <tt>'''1'''</tt>, with text coming after numbers. <tt>'''matter'''</tt> follows <tt>'''mat'''</tt> because although the first letters are the same, | + | :returns <tt>'''A'''</tt>. Cell A1 in the top row matches <tt>'''3'''</tt>. The top row must be sorted in mode <tt>'''1'''</tt>, with text coming after numbers. <tt>'''matter'''</tt> follows <tt>'''mat'''</tt> because although the first letters are the same, <tt>'''matter'''</tt> has more letters. |
− | <tt>'''HLOOKUP(4; A1: | + | <tt>'''HLOOKUP(4; A1:E2; 2; 1)'''</tt> |
: returns <tt>'''A'''</tt>. <tt>'''4'''</tt> would appear between <tt>'''3'''</tt> and <tt>'''5'''</tt> in the top row, so the position to the left is found. | : returns <tt>'''A'''</tt>. <tt>'''4'''</tt> would appear between <tt>'''3'''</tt> and <tt>'''5'''</tt> in the top row, so the position to the left is found. | ||
− | <tt>'''HLOOKUP(; A1: | + | <tt>'''HLOOKUP(1; A1:E2; 2; 1)'''</tt> |
− | : returns <tt>''''''</tt>. | + | : returns <tt>'''#N/A'''</tt>. <tt>'''1'''</tt> would appear before <tt>'''3'''</tt> in the top row, and there is no position to the left of that in the table. |
− | <tt>'''HLOOKUP(; A1: | + | <tt>'''HLOOKUP(7; A1:E2; 2; 1)'''</tt> |
− | : returns <tt>''''''</tt>. | + | : returns <tt>'''B'''</tt>. <tt>'''7'''</tt> would appear between <tt>'''5'''</tt> and <tt>'''cat'''</tt> in the top row, so the position to the left is found. |
+ | <tt>'''HLOOKUP("apple"; A1:E2; 2; 1)'''</tt> | ||
+ | : returns <tt>'''B'''</tt>. <tt>'''apple'''</tt> would appear between <tt>'''5'''</tt> and <tt>'''cat'''</tt> in the top row, so the position to the left is found. | ||
− | <tt>''''''</tt> | + | <tt>'''HLOOKUP("mate"; A1:E2; 2; 1)'''</tt> |
+ | : returns <tt>'''D'''</tt>. <tt>'''mate'''</tt> would appear between <tt>'''mat'''</tt> and <tt>'''matter'''</tt> in the top row, so the position to the left is found. | ||
+ | |||
+ | <tt>'''HLOOKUP("rat"; A1:E2; 2; 1)'''</tt> | ||
+ | : returns <tt>'''E'''</tt>. <tt>'''rat'''</tt> would appear after <tt>'''matter'''</tt> in the top row, so the position to the left is found. | ||
=== See also: === | === See also: === |
Revision as of 06:03, 4 May 2008
HLOOKUP
Returns a value from a table row, in the column found by lookup in the first row.
Syntax:
HLOOKUP(lookupvalue; datatable; rowindex; mode)
- lookupvalue is a value (number, text or logical value) to look up in the top row of the range/array datatable. When a value is matched in the top row, HLOOKUP returns the corresponding value (in the same column) in the rowindex^{th} row of datatable, where rowindex = 1 is the top row.
- If mode is 0 or FALSE, the top row of datatable may be unordered, and the first exact match is found (searching left to right).
- If mode is 1 or TRUE, or is omitted, the top row of datatable must be sorted, with numbers in ascending order appearing before text values in alphabetic order. Firstly an exact match is sought; if there is more than one exact match, the one found is not necessarily the leftmost. If there is no exact match, the position to the left of where value would appear in the top row is found; the #N/A error results if that position is not in the table.
- HLOOKUP will find exact matches treating lookupvalue as a regular expression, if regular expressions are enabled on the Tools - Options - OpenOffice.org Calc - Calculate dialog. This only makes sense, and should only be used, if mode is 0 or FALSE.
- If 'Search criteria + and <> must apply to whole cells' is enabled on the Tools - Options - OpenOffice.org Calc - Calculate dialog, lookupvalue must match the whole text in the cell; if not, it can match just part of the text.
- Matching is always case-insensitive - the case setting on the Tools - Options - OpenOffice.org Calc - Calculate dialog does not apply.
Examples:
In these examples, cells A1, B1, C1, D1, E1 contain 3, 5, cat, mat, matter, and cells A2, B2, C2, D2, E2 contain A, B, C, D, E.
HLOOKUP(3; A1:E2; 2; 0)
- returns A. Cell A1 in the top row matches 3. The top row happens to be sorted, but this is not necessary in mode 0.
HLOOKUP(4; A1:E2; 2; 0)
- returns #N/A. There is no match for 4.
HLOOKUP("CAT"; A1:E2; 2; 0)
- returns C. The difference between lower and upper case is ignored, so CAT matches cat.
HLOOKUP("m.t"; A1:E2; 2; 0)
- returns D if regular expressions are enabled (and #N/A if not). The dot '.' stands for 'any single character' in a regular expression, so m.t matches mat.
HLOOKUP(".at"; A1:E2; 2; 0)
- returns C if regular expressions are enabled. cat matches; so does mat, but the leftmost match is always found in mode 0.
HLOOKUP("at"; A1:E2; 2; 0)
- returns #N/A if 'Search criteria + and <> must apply to whole cells' is enabled and C otherwise. at does not match any whole cell contents, but it matches part of cat.
HLOOKUP(3; A1:E2; 2; 1)
- returns A. Cell A1 in the top row matches 3. The top row must be sorted in mode 1, with text coming after numbers. matter follows mat because although the first letters are the same, matter has more letters.
HLOOKUP(4; A1:E2; 2; 1)
- returns A. 4 would appear between 3 and 5 in the top row, so the position to the left is found.
HLOOKUP(1; A1:E2; 2; 1)
- returns #N/A. 1 would appear before 3 in the top row, and there is no position to the left of that in the table.
HLOOKUP(7; A1:E2; 2; 1)
- returns B. 7 would appear between 5 and cat in the top row, so the position to the left is found.
HLOOKUP("apple"; A1:E2; 2; 1)
- returns B. apple would appear between 5 and cat in the top row, so the position to the left is found.
HLOOKUP("mate"; A1:E2; 2; 1)
- returns D. mate would appear between mat and matter in the top row, so the position to the left is found.
HLOOKUP("rat"; A1:E2; 2; 1)
- returns E. rat would appear after matter in the top row, so the position to the left is found.