Difference between revisions of "Documentation/How Tos/Calc: HLOOKUP function"
From Apache OpenOffice Wiki
< Documentation | How Tos
(→Syntax:) |
(→Example:) |
||
Line 19: | Line 19: | ||
: Matching is <u>always case-insensitive</u> - the case setting on the '''Tools - Options - OpenOffice.org Calc - Calculate''' dialog does not apply. | : Matching is <u>always case-insensitive</u> - the case setting on the '''Tools - Options - OpenOffice.org Calc - Calculate''' dialog does not apply. | ||
− | === | + | === Examples: === |
− | <tt>'''HLOOKUP()'''</tt> | + | In these examples, cells A1, B1, C1, D1, E1 contain <tt>'''3'''</tt>, <tt>'''5'''</tt>, <tt>'''cat'''</tt>, <tt>'''mat'''</tt>, <tt>'''matter'''</tt>, and cells |
− | : returns <tt>''' '''</tt>. | + | A2, B2, C2, D2, E2 contain <tt>'''A'''</tt>, <tt>'''B'''</tt>, <tt>'''C'''</tt>, <tt>'''D'''</tt>, <tt>'''E'''</tt>. |
+ | |||
+ | [[Image:Calc_hlookup_example.png|center]] | ||
+ | |||
+ | <tt>'''HLOOKUP(3; A1:B5; 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>. | ||
+ | |||
+ | <tt>'''HLOOKUP(4; A1:B5; 2; 0)'''</tt> | ||
+ | : returns <tt>'''#N/A'''</tt>. There is no match for <tt>'''4'''</tt>. | ||
+ | |||
+ | <tt>'''HLOOKUP("CAT"; A1:B5; 2; 0)'''</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:B5; 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>. | ||
+ | |||
+ | <tt>'''HLOOKUP(".at"; A1:B5; 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>. | ||
+ | |||
+ | <tt>'''HLOOKUP("at"; A1:B5; 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>. | ||
+ | |||
+ | <tt>'''HLOOKUP(3; A1:B5; 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, it has more letters. | ||
+ | |||
+ | <tt>'''HLOOKUP(4; A1:B5; 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. | ||
+ | |||
+ | <tt>'''HLOOKUP(; A1:B5; 2; 1)'''</tt> | ||
+ | : returns <tt>''''''</tt>. | ||
+ | |||
+ | <tt>'''HLOOKUP(; A1:B5; 2; 1)'''</tt> | ||
+ | : returns <tt>''''''</tt>. | ||
+ | |||
+ | |||
+ | <tt>''''''</tt> | ||
=== See also: === | === See also: === |
Revision as of 05:13, 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:B5; 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:B5; 2; 0)
- returns #N/A. There is no match for 4.
HLOOKUP("CAT"; A1:B5; 2; 0)
- returns C. The difference between lower and upper case is ignored, so CAT matches cat.
HLOOKUP("m.t"; A1:B5; 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:B5; 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:B5; 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:B5; 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, it has more letters.
HLOOKUP(4; A1:B5; 2; 1)
- returns A. 4 would appear between 3 and 5 in the top row, so the position to the left is found.
HLOOKUP(; A1:B5; 2; 1)
- returns '.
HLOOKUP(; A1:B5; 2; 1)
- returns '.
'