Why does the hlookup function return 0? - excel-formula

Why does this hlookup function return 0?
A1 = 2, B1 = 0, C1 = 0
=hlookup(3,A1:C1,1,true)
Reading the description here: https://support.microsoft.com/en-us/office/hlookup-function-a3034eec-b719-4ba3-bb65-e1ad662ed95f, it says for the range parameter (the last parameter):
"If TRUE or omitted, an approximate match is returned. In other words, if an exact match is not found, the next largest value that is less than lookup_value is returned"
The next largest value is 2 (in A1) which is less than the lookup value (3).
Using MS Office 365 Apps for enterprise

HLOOKUP will require that the values be sorted when using TRUE:
https://support.microsoft.com/en-us/office/hlookup-function-a3034eec-b719-4ba3-bb65-e1ad662ed95f#:~:text=If%20range_lookup%20is%20TRUE%2C%20the%20values%20in%20the%20first%20row%20of%20table_array%20must%20be%20placed%20in%20ascending%20order
But we can use MAXIFS if they are numbers:
=MAXIFS(A1:C1,A1:C1,"<="&3)
OR XLOOKUP which does not care if sorted or not.
=XLOOKUP(3,A1:C1,A1:C1,"",-1)

The HLOOKUP function returns 0 because the value 3 is not found in the lookup range A1:A3. The HLOOKUP function performs a horizontal search, i.e., it searches for the lookup value vertically in the first row of the lookup range.
Since the lookup value 3 is not found in the range A1:A3, which only contains the values 2, 0, and 0, the HLOOKUP function returns the value specified in the result_vector argument, which in this case is 1. When the value is not found and the range_lookup argument is set to True (which it is in this case), the HLOOKUP function returns the closest match that is less than the lookup value. In this case, since there is no match, it returns 0.

Related

Return values equal to or less than and equal to or greater than lookup value in one-column table

I have a one-column table like this:
I want to find the value that is equal to or immediately less than a given lookup value, and the value that is equal to or immediately greater than a given lookup value. For example, suppose the lookup value is 13. Then the two outputs should be 10 and 15, respectively.
My attempt
I managed to obtain the standard value that is equal to or immediately less than the lookup value, by using the formula
=VLOOKUP(D1, A2:A6, 1, TRUE)
in cell D2, as shown in the following figure, where cell D1 contains the lookup value.
How can I obtain the standard value that is equal to or immediately greater than the lookup value?
If you try to use the LookUp function then you have to reorder the data from highest to lowest to make it work.
However, one way to avoid having the data sorted and still find the upper value and the lower value can be as follows:
D2= SMALL(A2:A6, COUNTIF(A2:A6, "<" & D1))
D3= SMALL(A2:A6, COUNTIF(A2:A6, "<=" & D1) +1)
However, a similar result can also be obtained by combining the SMALL function with INDEX and MATCH.
If you have Excel-365 then you can use XLOOKUP() function with match_mode 1 which indicates exact match or next higher values. Try-
=XLOOKUP(D1,A2:A6,A2:A6,,1)
More on excelject.

Excel calculation return wrong value

I'm building a simple POS to calculate item cost. I'm able to calculate item cost for Belts and Scarf but when i calculate item cost for jewellery
it return the wrong calculating.
The formula i'm using = B37 * VLOOKUP(B36, A2:B5, 2, TRUE)
Your items aren't sorted. You must sort items (A3:A5) A to Z.
Changing TRUE to FALSE changes the way VLOOKUP works:
If TRUE or omitted, an exact or approximate match is returned. If an
exact match is not found, the next largest value that is less than
lookup_value is returned. The values in the first column of
table_array must be placed in ascending sort order; otherwise, VLOOKUP
may not give the correct value. For more information, see Sort data.
If FALSE, VLOOKUP will only find an exact match. In this case, the
values in the first column of table_array do not need to be sorted. If
there are two or more values in the first column of table_array that
match the lookup_value, the first value found is used. If an exact
match is not found, the error value #N/A is returned.
Change the "TRUE" in your formula to "FALSE" and it should work.
= B37 * VLOOKUP(B36,A2:B5,2,FALSE)

How to find first non zero value in a column?

in column A first value is 0, second is 0, third is 17, fourth is 0 and fifth is 32
in this case , first non zero value is 17. how to calculate it by formula
In cell B1:
=INDEX(A1:A5,MATCH(TRUE,INDEX(A1:A5<>0,),0))
Explanation for the above formula:
The inner Index function evaluates the values in the A1:A5 range to either TRUE or FALSE based on the specified condition, that is, not equal to 0. So 17 and 32 evaluate to TRUE, everything else to FALSE. The Match function returns the row number where the first instance of the specified condition TRUE is. This occurs on the 3rd row of the range, so Match evaluates to 3. Finally, the outer Index functions returns the value at row number 3 in the specified A1:A5 range, which is 17.

Lookup value - #N/A result

I am using =IF(VLOOKUP(A2;$B$2:$B$11;B2;FALSE); TRUE; FALSE) to lookup a value in the column A.
As you can see my formula does not seem valid. Any suggestions why this is the case?
I appreciate your answer!
UPDATE
I am currently getting with =ISNUMBER(VLOOKUP(A2;$B$2:$B$11;1;FALSE)) only FALSE values. However I want to see True if there is a match:
Update
Using =Not(ISNA(VLOOKUP(A2;$B$2:$B$11;1;FALSE))) and =ISNUMBER(MATCH(A2, $B$2:$B$11, 0)) gives me the same result:
VLOOKUP takes the following arguments:
=VLOOKUP(lookup_value; table_array; col_index_num; [range_lookup])
lookup_value is what will be looked for in the first column of table_array.
table_array is the table in which the lookup_value and the value to be returned are.
col_index_num is the number indicating the nth column within table_array from which the value is to be returned from.
[range_lookup] (defaults to true) indicates the type of lookup, true being approximate and false being exact.
As such, if you want to find if A2 is in table $B$2:$B$11, you need to use a col_index_num of 1.
A simpler formula however would be with MATCH:
=ISNUMBER(MATCH(A2; $B$2:$B$11; 0))
MATCH returns the relative row number in which the value A2 is found. If there is a match, you get a number and hence ISNUMBER returns TRUE.
If you want to check if a value from column B exists in column A, then you have it reversed in the formula you used. Turning it around gives:
=ISNUMBER(MATCH(B2; $A$2:$A$11; 0))
You could also use COUNTIF, i.e. this formula in C2 copied down
=COUNTIF(A$2:A$11,B2)>0

finding the cell that is closest to the current cell with conditions

I have a row of values - 1,2,3,8,35,7,8,3,5,7,x
X is where I want the formula to be
I'd like to somehow get the value of the row which 8 is the closest to X (so not row 4 in this case, but row 7)
If I use match("8",A:A,0) I get the first match it finds.
How do I find the closest match to the cell where the calculation occurs?
You may use:
{=MATCH(2,1/(A1:A10=8))}
just remember it is an array function, so CTRL+SHIFT+ENTER must be used
The answer is based on a trick and behaviour of the MATCH function. I will allow myself to copy the explanation from ozgrid:
The magic here is actually in the MATCH function more than the array. Two interesting properties of the match function are being used here:
1) With MATCH, if no match is found, then the function will return the position of the last value in the array, so if you did =MATCH(8,{1,2,3,4,5,6,7,6,5,4,3,2,1}), your result is = 13, since there was no 8 to find in the array
2) MATCH will return the position of the last value, but won't return the position of an error (or of a blank value), so if you have =MATCH(8,{1,2,3,4,#DIV/0!,#DIV/0!,7,6,5,4,3,#DIV/0!,#DIV/0!}), your result is = 11, as the 3 in the 11th position is the last value in the array
So daddylonglegs' formula checks each cell against the target value with (A1:A13=B1) in the array formula, giving an array with TRUE (or 1) for the positions where the cells match, and with FALSE (or 0) for the rest. Dividing 1 by this results in 1s wherever that array was TRUE, and #DIV/0! wherever that array was false. So his formula is evaluated as
=MATCH(2,{#DIV/0!,1,#DIV/0!,#DIV/0!,#DIV/0!,#DIV/0!,1,#DIV/0!,1,#DIV/0!,#DIV/0!,#DIV/0!,#DIV/0!})
Since no '2' is found in the array, and the last value (1) is found in the 9th position, the MATCH returns 9
If you want the last match you can use this formula in A11
=MATCH(2,INDEX(1/(A1:A10=8),0))
[Note: if the values are numeric then 8 is OK - if they are text formatted numbers then you need "8"]
....or do you have values either side of the calculation cell (so nearest might be up or down)?

Resources