Can a countifs do this or do I need an array? - excel

I have a data range B5:L100.
In column B is a string identifier, say 'X' or 'Y'.
In columns C:L we have different people's names entered (never more than once per row).
I want to count how many times a person's name appears in rows where column B is 'X'. The following formula doesn't work (using "Max" as the example person to search for).
Can you advise on what will do this elegantly?
=COUNTIFS(C5:L100,"Max",B5:B100,"X")
I think an array formula might be in order, but I'm not too experienced on those.

One way to do this is to just do it column by column:
=COUNTIFS(C5:C100,"Max",B5:B100,"X")+COUNTIFS(D5:D100,"Max",B5:B100,"X")... etc
Does the trick, but not too elegant if you have loads of columns to look through. I'm sure there's a tidier way using an array formula.

=SUMPRODUCT((C5:L100="Max")*(B5:B100="X"))

Related

Find a value in a range (any of multiple column or rows) and return the value in far left most column of that row

Looking for something like a typical index match formula that can look to the right and return value to the left, but look at all columns in a range. Take below valid formula for example.
(Excel 2021.)
Finds A1's value in column D, and it returns value from column C.
=INDEX($C$1:$C$10,MATCH(A1,$D$1:$D$10,0))
In my ideal world I can Keep $D$1 and change $D$10 to $F$10 so it searches all columns D/E/F, and still returns C like below. However that does not work in our real world, any other ideas please? Thanks!
=INDEX($C$1:$C$10,MATCH(A1,$D$1:$F$10,0))
Update*
To clarify there are mix of letters and numbers. Also this table will be about 50k rows so hoping as simple as possible.
Also Column C will all be unique for sure, and D-F should be unique values but there is a chance a mistake and a few duplicates might be in.
You need MMUL() with INDEX(). Try below formula if you have Excel-365.
=FILTER(C1:C10,MMULT(--(D1:F10=A1),SEQUENCE(COLUMNS(D1:F1))))
For older version try
=INDEX($C$1:$C$10,LARGE(MMULT(--($D$1:$F$10=A1),TRANSPOSE({1,1,1}))*ROW($C$1:$C$10),1))
Since your INDEX/MATCH take from the same rows, you can first simplify your original search with
=XLOOKUP(A1,$D$1:$D$10,$C$1:$C$10)
XLOOKUP combines HLOOKUP and VLOOKUP with exact match being the default.
This will work for searching three rows
IFERROR(IFERROR(XLOOKUP(A1,$D$1:$D$10,$C$1:$C$10), XLOOKUP(A1,$E$1:$E$10,$C$1:$C$10)), XLOOKUP(A1,$F$1:$F$10,$C$1:$C$10))
We can name the columns colC, colD, colE, and colF and it becomes
IFERROR(IFERROR(XLOOKUP(A1,colD,colC), XLOOKUP(A1,colE,colC)), XLOOKUP(A1,colF,colC))
As with other lookups, this returns the first value or #N/A error.
This could be made more scalable for higher number of rows if we are allowed to add a column somewhere.

Search a column using entries from another column?

Columns A:D are First Name, Last Name, Address, City. I have about 85 rows of information.
I'm trying to determine if a CITY (Column D) occurs in my list of cities (on another sheet, about 150 rows). It can provide me this answer in say, Column E.
I know VLOOKUP won't work because I need it to tell me ALL occurrences of the city name on multiple rows. I've tried to make INDEX-MATCH fit, but I can't figure out how to get it to do what I need (or if it can do what I need). I thought SEARCH would help, but the results don't seem to match reality (I can find entries).
Is there a formula that can do this for me? Basically, I need to know which of the entries match my separate list of cities
Just use COUNTIF()? (Adjust G2:G4 to the separate list of cities you have)
=IF(COUNTIF($G$2:$G$4,D2),"Is in the list","Is NOT in the list")
Vlookup will work, but it is not the most efficient way.
=if(iserror(vlookup(A1,'the other sheet'!B:B,1,false)),"not found", "found")
Index/Match will work, but you only need the Match part, anyway
=if(isnumber(match(A1,'the other sheet'!B:B,0)),"found","not found")
The Countif suggested by BruceWayne is the most efficient formula.

Returning highest value in a column with certain header

I nedd help with creating a formula in excel. I have two spreadsheets. In Sheet1 I have a table, names of the items in row 1, names of the parameters in column A and each pair of Item and Parameter has a value. In spreadsheet 2 I have a list of the same Items but in random order and with repetitions (Sorting is out of question as it would hurt formulas in other spreadsheets). I need a formula that for each Item in spreadsheet2 would return a name of the parameter with the highest value for that item. It looks very similar to the example on the pictures
I was thinking about using something like =INDEX(sheet1!A:A;MATCH(MAX(?);?;0);1)
But in place of question marks I would have to put the column name in the formula or find a way to pass it a reference but I don't know how.
EDIT: I know how to do it in VBA but I would prefer to do it in a formula instead
Close, use another INDEX/MATCH to return the correct column:
=INDEX(sheet1!A:A,MATCH(MAX(INDEX(sheet1!A:S,0,MATCH(A2,sheet1!$1:$1,0))),INDEX(sheet1!A:S,0,MATCH(A2,sheet1!$1:$1,0)),0))
There are tidier ways but here is an initial:
=INDEX(Sheet1!$A$1:$A$12,MATCH(MAX(INDEX(Sheet1!$A$1:$F$12, ,MATCH($A2,Sheet1!$B$1:$F$1,0)+1)),INDEX(Sheet1!$A$1:$F$12, ,MATCH($A2,Sheet1!$B$1:$F$1,0)+1),0))

Sumproduct or Countif on a 2D matrix

I'm working on data from a population of people with allergies. Each person has a unique ExceptionID, and each allergen has a unique AllergenID (451 in total).
I have a data table with 2 columns (ExceptionID and AllergenID), where each person's allergies are listed row by row. This means that the ExceptionID column has repeated values for people with multiple allergies, and the AllergenID column has repeated values for the different people who have that allergy.
I am trying to count how many times each pair of allergies is present in this population (e.g. Allergen#107 & Allergen#108, Allergen#107 & Allergen#109,etc). To keep it simple I've created a matrix of 451 rows X 451 columns, representing every pair (twice actually because A/B and B/A are equivalent).
I somehow need to use the row name (allergenID) to lookup the ExceptionID in my data table, and count the cases where that matches the ExceptionIDs from the column name (also AllergenID). I have no problem using Vlookup or Index/Match, but I'm struggling with the correct combination of a lookup and Sumproduct or Countif formula.
Any help is greatly appreciated!
Mike
PS I'm using Excel 2016 if that changes anything.
-=UPDATE=-
So the methods suggested by Dirk and MacroMarc both worked, though I couldn't apply the latter to my full data set (17,000+ rows) because it was taking a long time.
I've since decided to turn this into a VBA macro because we now want to see the counts of triplets instead of pairs.
With the 2 columns you start with, it is as good as impossible... You would need to check every ExceptionID to have 2 different specific AllergenID. Better use a helper-table with ExceptionID as rows and AllergenID as columns (or the opposite... whatever you like). The helper table needs a formula like:
=COUNTIFS($A:$A,$D2,$B:$B,E$1)
Which then can be auto-filled. (The ranges are from my example, you need to change them to your needs).
With this helper-matrix you can easily go for your bigger matrix like this:
=COUNTIFS(E:E,1,INDEX($E:$G,,MATCH($I2,$E$1:$G$1,0)),1)
Again, you can auto-fill with this formula, but you need to change it, so it fits your needs.
Because the columns have the same ID2 (would be your AllergenID), there is no need to lookup them because E:E changes automatically with the auto-fill.
Most important part of the formulas are the $ which should not be messed up, or you can not auto-fill it.
Picture of my self-made example (formulas are from the upper left cell in each table):
If you still have any questions, just ask :)
It can be done straight from your original set-up with array formulas:
Please note that array formulas MUST be entered with Ctrl-Shift-Enter, before copying across and down:
In the example pic, I have NAMED the data ranges $A$2:$A$21 as 'People' and $B$2:$B$21 as 'Allergens' to make it a nicer set-up. You can see in the formula bar how that looks as a formula. However you could use the standard references like this in your first matrix cell:
EDIT: silly me, N function is not needed to turn the booleans into 1's and 0's, since multiplying booleans will do the trick. Below formula works...
SUM(IF(MATCH($A$2:$A$21,$A$2:$A$21,0)=ROW($A$2:$A$21)-1, NOT(ISERROR(MATCH($A$2:$A$21&$E2,$A$2:$A$21&$B$2:$B$21,0)))*NOT(ISERROR(MATCH($A$2:$A$21&F$1, $A$2:$A$21&$B$2:$B$21,0))), 0))
Then copy from F2 across and down. It can be perhaps improved in technique with sumproduct or whatever, but it's just a rough example of the technique....

How Do I Copy The Cell Containing a Substring?

I have a list of alphanumeric inventory items in Column A. They are sorted ascending by value.
In Column B, I have an unsorted list of the inventory items filenames.
I'd like to place a formula in Column C that finds the cell in Column B that contains a substring that matches Column A.
I've experimented with several forms of VLOOKUP and INDEX/MATCH, but the best I've gotten is an index number of the matching cell. That isn't quite what I need, but its the closest I've gotten.
I'd really like to get the entire value of the cell in Column B.
I know of one way to do this, and one additional resource that might make your life much easier.
First, If I had the index of the matching cell, I would simply use an indirect function. They're used like this:
indirect("string reference to a cell")
In your case, cell C1 would contain
=indirect("A" & B1)
Second, the resource I have in mind is something I read long, long ago, but stuck in my mind as absolutely brilliant. It uses the lookup function, which only exists for backwards compatibility but has turned out to have hidden utility. See this MrExcel.com page for more info. Barry Houdini's answer is, to prevent link rot, repeated here without the question itself: =LOOKUP(2^15,SEARCH(D$2:D$10,A2),E$2:E$10)

Resources