excel, matching with greater than and lesser than and static values - excel

I am trying to do in Excel what should be done in a database. I have a spreadsheet with raw data and which I am trying to query based on criteria. Given the following example table:
A B C D E F
1 Red up 1 4 dn 5
2 Blu up 5 9
3 Yel dn 1 4
4 Gre dn 5 9
I would like to return the value of column A that meets the criteria of E1 and F1 where E1 is found in column B and F1 is found equal or between the values in columns C and D. In the example, I would like to return "Gre".
I have been pulling my hair out with INDEX and MATCH functions and I can get part of my task done, but have found nothing extensible to solve the total solution.
Thank you in advance for your help!

Please try this...
=IFERROR(INDEX($A$1:$A$4,MATCH(1,INDEX(($B$1:$B$4=$E$1)*($C$1:$C$4<=$F$1)*($D$1:$D$4>=$F$1),),0)),"")

If you don't mind adding headings to your raw data.
You could use this formula:
=DGET($A$1:$D$5,"Field 1",$E$1:$F$2)
A1:D5 being your database.
Field 1 is the field to return values from.
E1:F2 is your criteria (field name and value to look for in that field).
https://support.office.com/en-gb/article/DGET-function-455568bf-4eef-45f7-90f0-ec250d00892e
As noted by #Vityata this won't work for the OP - looking for the value 6 would return a #VALUE error rather than Gre.
A couple of updates would allow it to work:
Updating the formula to: =DGET($A$1:$D$5,"Field 1",$E$1:$G$2)
Updating the table to:
The values in F2 and G2 are calculated as:
="<=" & $H$2 and =">=" & $H$2
This example would then return Yel when 1 is entered in cell H2.

I liked the question, thus I have elaborated a bit on the sktneer's answer.
The reason, it works is because we are looking for truth (a.k.a. 1) in the following formula:
=MATCH(1;
INDEX(($B$1:$B$4=$E$1)*($C$1:$C$4<=$F$1)*($D$1:$D$4>=$F$1););0)
Like this:
Then with an INDIRECT we may achieve the answer:
=INDIRECT(ADDRESSE(I1;1))
If you want it in one formula, it should be like this:
=INDIRECT(ADDRESS(
MATCH(1;INDEX(($B$1:$B$4=$E$1)*($C$1:$C$4<=$F$1)*($D$1:$D$4>=$F$1););0);
1))

Related

counting duplicates and assigning label

In Excel suppose I have a table with the following two columns and following data:
ID Value
1 6
1 2
1 1
2 4
3 5
In excel what I would like to do is write the word duplicate in a third column (say result) when the id is duplicate and is not the highest value.
In this example duplicate would be written next to Value(2),ID(1) and Value(1),ID(1). Value(6), ID(1) would not have duplicate written next to it becasue it has the highest value out of all the ID(1)'s.
Is there an excel formula I can use to do this? If not what VBA would I need? In reality this is a large database and there will be more than 3 duplicates.
The result should look like this:
ID Value
1 6
1 2 Duplicate
1 1 Duplicate
2 4
3 5
Not sure if this is correct. But please correct me if I am wrong.
=IF(MIN($A$2:$A$6 = MIN($B$2:$B$6)), "duplicate", "")
This array formula should work (Ctrl+Shift+Enter) to confirm, though if you have lots of data could be rather slow.
=IF(B2=MAX(IF($A$2:$A$6=A2,$B$2:$B$6)),"","Duplicate")
if the duplicates are in column A, the cell B3 could read: (if ID are decreasing)
=if(COUNTIF($A$1:$A2,A3)>0,"Duplicate #" & COUNTIF($A$1:$A3,A3),"")
does this help?

Sum row based on criteria across multiple columns

I have googled for hours, not being able to find a solution to what I need/want. I have an Excel sheet where I want to sum the values in one column based on the criteria that either one of two columns should have a specific value in it. For instance
A B C
1 4 20 7
2 5 100 3
3 100 21 4
4 15 21 4
5 21 24 8
I want to sum the values in C given that at least one of A and B contains a value of less than or equal to 20. Let us assume that A1:A5 is named A, B1:B5 is named B, and C1:C5 is named C (for simplicity). I have tried:
={SUMPRODUCT(C,((A<=20)+(C<=20)))}
which gives me the rows where both columns match summed twice, and
={SUMPRODUCT(C,((A<=20)*(C<=20)))}
which gives me only the rows where both columns match
So far, I have settled for the solution of adding a column D with the lowest value of A and B, but it bugs me so much that I can't do it with formulas.
Any help would be highly appreciated, so thanks in advance. All I have found when googling is the "multiple criteria for same column" problem.
Thanks. That works. Found another one that works, after I figured out that excel does not treat 1 + 1 = 1 as I learnt in discrete mathematics, but as you say, counts the both the trues. Tried instead with:
{=SUM(IF((A<=20)+(B<=20);C;0))}
But I like yours better.
Your problem that it is "summing twice" in this formula
={SUMPRODUCT(C,((A<=20)+(C<=20)))}
is due to addition turning first TRUE plus the second TRUE into 2. It is not actually summing twice, because for any row, if only one condition is met, it would count that row only once.
The solution is to transform either the 1 or the 2 into a 1, using an IF:
={SUMPRODUCT(C,IF((A<=20)+(C<=20))>0, 1, 0)}
That way, each value in column C would only be counted at max once.
Following this site you could build up your SUMPRODUCT() formula like this:
=SUMPRODUCT(C,SIGN((A<=20)+(C<=20)))
So, instead of a nested IF() you control your or condition with the SIGN()function.
hth
If you plan to use a large set of data then it is best to use the array formula:
{=SUM(IF((A1:A5<=20)+(B1:B5<=20),C1:C5,0))}
Obviously adjust the range to suit the data set, however if the whole of each column is to form part of the formula then you can simply adjust to:
{=SUM(IF((A:A<=20)+(B:B<=20),C:C,0))}
This will perform the calculation on all rows of data within the A, B and C columns. With either example remember to press Ctrl + Shift + Enter in order to trigger the array formula (as opposed to typing the { and }).

Formula to find match in two-dimensional range

I need a formula that will look up a value in a 2-dimensional range and return the coordinates or cell address of the matching cell. For example:
R A B C
1 John Matt Pete
2 Sara Bret Chad
3 Lila Maya Cami
I want to search the range A1:C3 for Chad and return C2 or 2,3. How can I accomplish this using Excel formulas? (I'll actually end up applying this to Google Sheets).
Thanks!
Old question, but I thought I'd share a much simpler and elegant answer here that doesn't involve helper columns or complicated formulas, so that more people will get things done easier. Assuming that the table contains unique values and that you use E1 to store your search string Chad and E2 to display the result:
if you want the row and column result of 2,3 in E2:
=SUMPRODUCT((A1:C3=E1)*ROW(A1:C3)) & "," & SUMPRODUCT((A1:C3=E1)*COLUMN(A1:C3))
if you want the R1C1 style cell address string of C2 in E2:
=ADDRESS(SUMPRODUCT((A1:C3=E1)*ROW(A1:C3)),SUMPRODUCT((A1:C3=E1)*COLUMN(A1:C3)))
if you want the found cell's contents of Chad in E2:
=INDIRECT(ADDRESS(SUMPRODUCT((A1:C3=E1)*ROW(A1:C3)),SUMPRODUCT((A1:C3=E1)*COLUMN(A1:C3))))
How things work:
SUMPRODUCT returns in this case the sum of the products between a boolean array of TRUE (searched value found in cell) and FALSE (searched value not found in cell) for every cell in the table and the corresponding row/column (absolute) numbers of those cells in the sheet; thus, the result is essentially the row/column (absolute) number of the cell where the value has been found, since TRUE=1 and FALSE=0 in mathematical terms
ADDRESS returns a cell's address as text (not as reference!)
INDIRECT returns the reference corresponding to a cell's text address
Source and credit goes to: this answer by XOR LX. Could have added the link in a comment, mentioning the duplicate question, but I wanted to expand and explain the answer a little bit, therefore more characters were needed.
Assuming you're using Excel 2007 and above.
You will need a helper column. If your table looks like in your example, in cell D1 write:
=IFERROR(MATCH($E$1,$A1:$C1,0),0)
And drag it down. Then in cell E1 write your search value ("Chad" for instance). Then you have your search result in cell E2 with this formula:
=IF(MAX($D:$D)=0,NA(),MATCH(MAX($D:$D),$D:$D,1)&","&MAX($D:$D))
If you want a simpler solution, it is possible to use only one helper (or not at all, at the cost of a complicated formulae).
Let's say I take your example. I will use the D column to display result :
In D1, I put the name I want to find : Chad
In D2, I put the helper that will return an Index of the value searched (-1 if not found) : =IFERROR(MATCH(D1,SPLIT(TEXTJOIN(";",TRUE,A1:C3),";"),0),-1)
In D3, I put the formulae to get the row,column value (FALSE if not found) : =IF(D2<>-1,ROUNDUP(DIVIDE(D2,COLUMNS(A1:C3))) & "," & IF(MOD(D2,COLUMNS(A1:C3))=0,COLUMNS(A1:C3),MOD(D2,COLUMNS(A1:C3))))
If you really want to use only one formulae, it is possible in D3 to replace all references to D2 by the formulae used in D2.
This formula returns the row and column number of a given value in a two-dimensional array.
=LET(
array, B2:D4,
findvalues, C7,
arrayrows, ROWS(array),
arraycols, COLUMNS(array),
rowindex, SEQUENCE(arrayrows*arraycols,,1,1/arraycols),
colindex, MOD(SEQUENCE(arrayrows*arraycols,,0),arraycols)+1,
flatarray, INDEX(array,rowindex,colindex),
valueflatindex, MATCH(findvalues,flatarray,0),
valuerow, ROUNDUP(valueflatindex/arraycols,0),
valuecol, MOD(valueflatindex-1,arraycols)+1,
absvaluerow, MIN(ROW(array))+valuerow-1,
absvaluecol, MIN(COLUMN(array))+valuecol-1,
CHOOSE({1,2},absvaluerow,absvaluecol)
)
A B C D E
1
2 John Matt Pete
3 Sara Bret Chad
4 Lila Maya Cami
5
6
7 find: Chad
8 formula: 3 4
More precisely, this formula scans a given array row by row and returns the address of the first occurrence of a given value.
If you need the row and column numbers relative to the array's top left cell, then in CHOOSE(...), instead of absvaluerow/absvaluecol, use valuerow/valuecol.
If you want the values to be comma separated and in one cell, instead of CHOOSE(...), use absvaluerow & "," & absvaluecol
If your Excel version does not support the latest functions, such as LET, the formula should still work if you rewrite it so that it does not use the LET variables.
Find Multiple Values
You can also find multiple values in an array using this formula as explained in my answer in this thread.

excel 2007 X Y lookup

Good morning. I have been searching for some code either a formula or VBA to lookup values in a table. I have two tables that I am looking to migrate data from the old with the new. Both tables are relatively the same.
A B C D E
1 Store 1234 2345 3456 4567
2 1234 0 5 10 15
3 2345 5 0 20 25
4 3456 10 20 0 35
5 4567 15 25 35 0
It is basically a mileage table with location to location distance. I am looking for a way to take the values from the old table into the new when the row columns dont match up exactly due to new locations added.
I know how to do a vlookup, but that dosent do what Im looking for... I want a way for a formula or vba to something like "find Value in B2 "1234" where it intersects the value in D1 "2345" = D2 "5"
Should be able to do a Index and Match combo to find it. I1 = From, K1 = To you can just type in the locations into those cells and get the range out you want.
=INDEX(A1:E5, MATCH(I1,A1:E1,0), MATCH(K1,A1:A5,0))
Can you implement IF formula to vlookup? In abstract: =IF((vlookup)=something),do this, else)
I would be hapy to look into this issue further, if you could provide complete BEFORE data and expected AFTER data.
Also, maybe =DGET would work for you?
I used this formula slightly modified and it worked fine,
=INDEX($A$1:$G$7, MATCH(B12,$A$1:$A$7,0), MATCH(C12,$A$1:$G$1,0))
the first part of the equation is the entire table including the X and Y coords. the next two parts are the X and Y coords (you can swap these round if you wish)
so the requested information for x and y was put in cells C12 and B12 and all those cells below...
then I used the dollar symbol to fix all the other look up cells, so i could use auto fill, and entered this in E12 (this is where the result will appear.
You can see the table in the attached (or could if I was allowed to attach it). I am looking up B12 (3) in column A then looking up C12 (6) in row 1 and returning the value at the intersection (in this case returns "r").
or there would be an attachment if the stupid web page did not insist on having me obtain a 10 reputation... hopefully you get the idea...

VLookup throwing an error

So I have a sheet similar to this:
A B C D E F
1 Name Age Number Gender Player player No.
2 Droid12 11 M Droid12 F3
3 R2D2 13 M C3P0 F12
4 C3P0 12 F Bot13 Y7
5 YVH7707 11 F J34 Z2
6 Bot13 15 M
7 Slim33 13 F
8 ABot43 14 F
9 DBo11 11 M
10 J34 12 M
I am trying to fill in Column C with the player number, if the person in question has one (Imagine that the sheet is thousands of time this large).
I have the following VLookup function in each cell in C (copied down from C1, of course):
=VLOOKUP(A2, $E$2:$F$5, 2,FALSE)
And am getting the result:
#N/A
When I try to step through, I get the error
Sheet1!$A$2 = Droid12: The cell currently being evaluated contains a
constant
Anyone have an idea what I'm doing wrong?
Thank you!
EDIT
I've tried some of these fixes, with no positive results. I tried this in C1-C3:
C2 contains:
=IF(A2=E2, F2, FALSE)
With the result that cell C2 contains the value Droid12
C3 contains:
=VLOOKUP(A2, $E$2:$F$3, 2, FALSE)
and is getting a #N/A error (with the same error:
Sheet1!$A$2 = Droid12: The cell currently being evaluated contains a
constant
The values are all standardized (Trimmed,etc...), and there is definitely a match in the range I'm comparing to, so I really can't see what I'm doing wrong. Anyone ever experience this before?
edit 2
I fixed it, turns out I had Player No. In column E and Player in Column F, so the comparison for some reason was not running correctly. I switched those two entities around, and the VLOOKUP worked fine. Weird, but I'm not complaining. Thanks to everyone who tried to help!
Usually it happens if there is no exact match. Try to use trim and wildcard chars to allow matching to skip spaces. For example:
=VLOOKUP(CONCATENATE("*",TRIM(A2),"*"), $E$2:$F$5, 2,FALSE)
Use this formula:-
=IF(ISERROR(VLOOKUP(A2,E:F,2,FALSE)), "",VLOOKUP(A2,E:F,2,FALSE))
In Simple, formula is
=VLOOKUP(A2,E:F,2,FALSE)
Issue is caused due to the Range, which is mentioned as $E$2:$F$5.
When you are trying to drag the formula for the rest of cells, Range is getting updated wrongly. This is causing issue.
The Cell which doesn't match the actual value then VLOOKUP returns #N/A. To overcome this, I have placed a IF condition to check any error and display empty if fails otherwise the value.
Sometimes the constant error can be avoided if you have the reference data on the furtherest left-hand column on all sheets.
i.e.
Column A1 = Description plus info A2:etc (Sheet 1 and Sheet 2)
The N/A is the result of the name you are looking up not being in the list. This is normal behavior for the VLOOKUP function.
To eliminate the NA being displayed try this formula:
=IF(ISNA(VLOOKUP(A2,$E$2:$F$5, 2, FALSE)),"",VLOOKUP(A2,$E$2:$F$5, 2, FALSE))
This checks to see if the result of the lookup is N/A. If it is, then display blank (""). If the result is not NA then display the lookup value.
One issue with this solution is the lookup will be performed twice on each record that is found (once to check if it is N/A and once again to display the value (although Excel may be optimizing for this situation).

Resources