How can I tell if more than one 'IFS' condition is 'TRUE' and not just the first match? - excel

In Excel 365 I'm using an "IFS" statement to scan through a number of columns to find out if a cell's value is in any of the columns. I believe "IFS" will process all your conditions until it reaches the first one that is "TRUE" then output. However, I'd like to be able to find ALL instances where my condition is true and output or evaluate them all somehow. Is there a way to do this with IFS (or some other method)? I think I'd like to output the matching value for each true condition in a separate row, but anything that could help me see how many matched and/or which column each match is in would be helpful.
The code I have is a bit much to share as my columns are in other workbooks, so I'll just share a close example. This formula would be in a cell that outputs the match, column D below.
A | B | C | D | E
------------------------------------
ColA | Col1 | Col2 | Formula | Notes
------------------------------------
1 | 1 | 2 | 1 | Two matches in same column (Col1)
2 | 1 | 2 | 2 | Two matches in same column (Col2)
3 | 3 | 3 | 3 | Two matches in diff column (Col1 & Col2)
=IFS(
NOT(ISERROR(MATCH(INDIRECT("A"&(ROW())),INDIRECT("B:B"),0))),
INDEX(INDIRECT("B:B"),MATCH(INDIRECT("A"&(ROW())),INDIRECT("B:B"),0)),
NOT(ISERROR(MATCH(INDIRECT("A"&(ROW())),INDIRECT("C:C"),0))),
INDEX(INDIRECT("C:C"),MATCH(INDIRECT("A"&(ROW())),INDIRECT("C:C"),0))
)
Of course the expected output is to dump the matching value of the first condition that's true, but I'd like to output all instances the condition is true in separate rows if possible. Maybe something like this...
A | B | C | D | E
------------------------------------
ColA | Col1 | Col2 | Formula | Notes
------------------------------------
1 | 1 | 2 | 1 | Two matches in same column (Col1)
... | ... | ... | 1 | Two matches in same column (Col1)
2 | 1 | 2 | 2 | Two matches in same column (Col2)
... | ... | ... | 2 | Two matches in same column (Col2)
3 | 3 | 3 | 3 | Two matches in diff column (Col1 & Col2)
... | ... | ... | 3 | Two matches in diff column (Col1 & Col2)
In the above and in my actual case the '...' would display what's in the column of that particular row match, which may vary from one row to another row throughout the worksheets. Basically, column D in the example would be on a separate 'results' sheet with the same amount of columns and column value types as all the 'data' sheets being searched. Furthermore, each column of the 'results' sheet would be a formula scanning that one specific column in all sheets, but only outputting the given column value of the matched row. Something like below...
DATA SHEET
A | B | C
----------------------
FName | LName | Amount
----------------------
John | Doe | 10
Jane | Doe | 4
Jack | Black | 10
RESULTS SHEET
(all cells are formulas)
A | B | C
----------------------
FName | LName | Amount
----------------------
John | Doe | 10 < matching value in C
Jack | Black | 10 < but different A & C
I hope that last part answered any "why" questions. ;)
ADDITION (7/25/19):
Below is the complete formula I'm using on sheets like above, but with more columns. It works well with the exception of my requirement to know where ALL matches occur and not just the first match on the IFS statement. Column "F" is the column I'm matching to output the corresponding value from the column cell on the match's row as found on the data sheets (5 sheets) to the formulated 'results' sheet, as displayed above. The only thing that changes in the formula between cells is the "A:A" to "B:B" etc., including "F:F" (the column with the value to be "MATCHED" from "SOURCES!$B$2"), which I made the last condition in the formula case nothing is found in the other data sheets, pasting its own data in lieu of something like 0, N/A, or FALSE.
=IFS(
NOT(ISERROR(MATCH(INDIRECT((SOURCES!$B$2)&"F"&(ROW())),INDIRECT((SOURCES!$B$3)&"F:F"),0))),
INDEX(INDIRECT((SOURCES!$B$3)&"A:A"),MATCH(INDIRECT((SOURCES!$B$2)&"F"&(ROW())),INDIRECT((SOURCES!$B$3)&"F:F"),0)),
NOT(ISERROR(MATCH(INDIRECT((SOURCES!$B$2)&"F"&(ROW())),INDIRECT((SOURCES!$B$4)&"F:F"),0))),
INDEX(INDIRECT((SOURCES!$B$4)&"A:A"),MATCH(INDIRECT((SOURCES!$B$2)&"F"&(ROW())),INDIRECT((SOURCES!$B$4)&"F:F"),0)),
NOT(ISERROR(MATCH(INDIRECT((SOURCES!$B$2)&"F"&(ROW())),INDIRECT((SOURCES!$B$12)&"F:F"),0))),
INDEX(INDIRECT((SOURCES!$B$12)&"A:A"),MATCH(INDIRECT((SOURCES!$B$2)&"F"&(ROW())),INDIRECT((SOURCES!$B$12)&"F:F"),0)),
NOT(ISERROR(MATCH(INDIRECT((SOURCES!$B$2)&"F"&(ROW())),INDIRECT((SOURCES!$B$13)&"F:F"),0))),
INDEX(INDIRECT((SOURCES!$B$13)&"A:A"),MATCH(INDIRECT((SOURCES!$B$2)&"F"&(ROW())),INDIRECT((SOURCES!$B$13)&"F:F"),0)),
NOT(ISERROR(MATCH(INDIRECT((SOURCES!$B$2)&"F"&(ROW())),INDIRECT((SOURCES!$B$14)&"F:F"),0))),
INDEX(INDIRECT((SOURCES!$B$14)&"A:A"),MATCH(INDIRECT((SOURCES!$B$2)&"F"&(ROW())),INDIRECT((SOURCES!$B$14)&"F:F"),0)),
NOT(ISERROR(MATCH(INDIRECT((SOURCES!$B$2)&"F"&(ROW())),INDIRECT((SOURCES!$B$2)&"F:F"),0))),
INDEX(INDIRECT((SOURCES!$B$2)&"A:A"),MATCH(INDIRECT((SOURCES!$B$2)&"F"&(ROW())),INDIRECT((SOURCES!$B$2)&"F:F"),0))
)
My formulated "results" workbook also has a worksheet named "SOURCES" that I use to paste file names to connect all the data sheets corresponding columns.
Btw, I'm using this as a tool to 'un-merge' customer data between profiles in our LIVE site/database after obtaining all the tables and columns the customer key has been found (using SQL) to then compare it (using Excel) to our TEST site so I can pull apart the data that doesn't belong on the 'kept' record from the LIVE merge. In this case there were 3 records merged. Two records have a profile in the TEST site, while the kept record from the LIVE site actually does not have a TEST record, giving me 5 sheets of data to examine.

Suppose your data starting from the range A2:C2
I thing this formula can help you,
Array Formula (Use Ctrl+Shift+Enter)
=INDEX($A2:$C2,MATCH("OK",IF(ISNUMBER($A2:$C2),"OK",""),0))

Related

Sum All VLOOKUP Matches

I have a sheet where I am recording what I eat:
Another where I keep an index of values to lookup
I tried
=SUM(VLOOKUP('Sheet1'!A2:A11,'Sheet2'!A2:E11,2,FALSE))
but that only returned the first match, so then I tried
=SUMPRODUCT(SUMIF('Sheet1'!A2:A11,'Sheet2'!A2:A11,'Sheet2'!B2:B11))
but that isn't working either.
does anyone have a solution, where I can also multiply the value of the return match by the # of servings in the first sheet?
Thanks!
If you want a single output of calories through SUMPRODUCT then you can use
=SUMPRODUCT(B2:B11*IFERROR(VLOOKUP(A2:A11,Sheet2!A2:B11,2,0),0))
If you are sure that all entries on Sheet 1 can be located on Sheet 2 then you can drop IFERROR portion like
=SUMPRODUCT(B2:B11*VLOOKUP(A2:A11,Sheet2!A2:B11,2,0)).
Beware that if a value is not found in Sheet 2 then formula will produce wrong result as IFERROR will multiply the serving quantity with 0.
I combine 2 tables into one sheet, Table 1 housed in Column A & B and Table 2 housed in Column D & E
In G2, "Total Serving Colories" enter formula :
=SUMPRODUCT(VLOOKUP(T(IF({1},A2:A12)),D2:E12,2,FALSE)*B2:B12)
It's not super-clear what you're trying to get at. But defining the "Calories Per Serving" in a range called "cals",
+---+---------+-----+--------------------------------+
| | A | B | C |
+---+---------+-----+--------------------------------+
| 1 | egg | 3 | =(VLOOKUP(A2,cals,2,FALSE))*B2 |
| 2 | oatmeal | 1.5 | =(VLOOKUP(A3,cals,2,FALSE))*B3 |
| 3 | shrimp | 2 | =(VLOOKUP(A4,cals,2,FALSE))*B4 |
+---+---------+-----+--------------------------------+
Results in:

INDEX MATCH formula stops working when creating a table

Let's say I have the folowing values setup in excel:
1 | 2 | 3 |
a | b | c |
Above thess values I have a INDEX MATCH formula that looks up a translation of the numbers 1-3:
=INDEX(PartnersProjects[Name];MATCH(K1;PartnersProjects[ID];0))
K1` is the reference to value "1" and picks up "Test1" with current output:
Test1 | Test2 | Test3 |
1 | 2 | 3 |
a | b | c |
But when I convert the 2 rows with according 3 colums (1-3 and a-c) to a table, the formula in the row above stops working some how. How to solve this?
When you make the row 1, 2, 3 as the header row of a table, this forces Excel to evaluate it as a string because these becomes the names of the columns.
You have two solutions:
1- Convert the searched value (K1) to numeric before matching:
=INDEX(PartnersProjects[Name],MATCH(VALUE(K1),PartnersProjects[ID],0))
' ^^^^^^^^^
2- Convert the search range to text, but then you will need an ^array (CSE) formula:
=INDEX(PartnersProjects[Name],MATCH(K1,TEXT(PartnersProjects[ID], "0"),0))
' Ctrl+Shift+Enter ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To to arrange non sequencial IDs in order

I have three excels. All containing same IDs and phone numbers may be differ, Id is alphanumeric and we have total 10,000 records, but IDs are not present in same sequence across all excel sheets.
For example:
If one ID is present in row 2000 in sheet 1, same ID is present at 3200 row and in sheet 3 it is present in row 5200.
Sheet 1 Sheet2
ID | Contact Number| | ID | Contact Number |
MP-XX-098 | 89652395 | | KJ-OP-98 | 3323241 |
KJ-OP-98 | 3323241 | | MP-XX-098 | 89652395 |
OP-MK-09 | 9632211 | | UI-32-09 | 3234521 |
UI-32-09 | 3234521 | | OP-MK-09 | 9632211 |
I need to create single excel to find which system is having different records.
for example
Sheet
ID | Contact Number(1) | contact number(2) |Contact Number(3) |
MP-XX-098 | 89652395 | 89652395 | 89652395 |
KJ-OP-98 | 3323241 | 3323241 | 3323241 |
OP-MK-09 | 9632211 | 9632211 | 9632211 |
UI-32-09 | 3234521 | 3234521 | 3234521 |
Please note I already tried sort A-Z but it is not working.
Vlookup: use vlookup to find the corresponding value in each sheet:
ID | Contact Number(1) | contact number(2) |Contact Number(3) |
MP-XX-098 | =vlookup(a2,sheet1!$A$2:$b$100, 2, FALSE)| =vlookup(a2, sheet2!$A$2:$b$100, 2,FALSE)| 89652395 |
vlookup will search a range for a value (in this case ID) and return the nth column of the row where the value is found.
In this case the range to search is sheet1!$A$2:$b$100, the value to fins in in the first column: a2 and we need the value in the 2nd column: 2
To clarify Ken's answer a bit...
What you'll likely want to do is copy the IDs to a new sheet in, say, column A. then in columns B, C, and D, you'll put formulas such as Ken posted.
Note that Ken's formulas have a typo -- the search value comes first and then the search range. See this page at office.com for more info. So they really should be:
=vlookup($a2, sheet1!$A$2:$b$10001, 2, FALSE)
The first parm for vlookup is the cell address of the value you want to look up. That's the one in the current sheet, over in column A. If your first one is on row 2, then you'd use $A2 in your vlookup formulas. You want the $ before the A so that it always looks in column A, but not in front of the 2 because you want it to use the value on the same row as the formula. (So you can do this in cell B2 and copy it to C2 and D2, then use Fill Down to copy the formulas to all the rows.)
The second parm for vlookup is the search range -- that will be the range containing the ID and Contact Number in each of the other sheets. (e.g., if your ID is in column A and Contact Number in column B and they start on row 2 and there are 10k records, you'd use sheet1!$A$2:$B:10001 where "sheet1" is the name of the first worksheet.)
The third parm is the column in your search range from which you want to copy your value -- in this case, it's the contact number in the second column of the search range. (Note that this the column of the search range, not of the worksheet.)
The last parm, FALSE, just says to use an exact match, rather than find the closest.
Then, if you want to flag those rows where there is a discrepancy (so you can just scan them to find the problems), use something like this in Column E:
=IF(OR($B5 <> $C5,$B5 <> $D5), "***", "")
This will put three asterisks (***) in column E for each row where one of the contact numbers differs from one of the others.
Hope this helps!

Counting the number of older siblings in an Excel spreadsheet

I have a longitudinal spreadsheet of adolescent growth.
ID | CollectionDate | DOB | MOTHER ID | Sex
1 | 1Aug03 | 3Apr90 | 12 | 1
1 | 4Sept04 | 3Apr90 | 12 | 1
1 | 1Sept05 | 3Apr90 | 12 | 1
2 | 1Aug03 | 21Dec91 | 12 | 0
2 | 4Sept04 | 21Dec91 | 12 | 0
2 | 1Sept05 | 21Dec91 | 12 | 0
3 | 1Aug03 | 30Jan89 | 23 | 0
3 | 4Sept04 | 30Jan89 | 23 | 0
This is a sample of how my data is formatted and some of the variables that I have. As you can see, since it is longitudinal, each individual has multiple measurements. In the actual database there are over 10 measurements per individual and over 250 individuals.
What I am wanting to do is input a value signifying the number of older brothers and older sisters each individual has. That is why I have included the Mother ID (because it represents genetic relatedness) and sex. These new variable columns would just say how many older siblings of each sex each individual has. Is there a formula that I could use to do this quickly?
=COUNTIFS($B:$B,"<>"&$B2,$H:$H,$H2,$AI:$AI,$AI2,$J:$J,"<"&$J2)
Create a column named Distinct with this formula
=1/COUNTIF([ID],[#ID])
Then you can find all the older 0-sexed siblings like this
=SUMPRODUCT(([DOB]>[#DOB])*([MOTHERID]=[#MOTHERID])*([Sex]=0)*([Distinct]))
Note that I made the data a Table and used table notation. If you're not familiar [COLUMNNAME] refers to the whole column and [#COLUMNNAME] refers to the value in that column on the current row. It's similar to saying $A:$A and A2 if you're dealing with column A.
The first formula gives you a value to count that will always result in 1 for a particular ID. So ID=1 has three lines and Distinct will result in .33333 for each line. When you add up the three lines you get 1. This is similar to a SELECT DISTINCT in Sql parlance.
The SUMPRODUCT formula sums [Distinct] for every row where the DOB is greater than the current DOB, the Mother is the same as the current Mother, and the Sex is zero.
I have a possible solution. It involves adding two columns -- One for "# older siblings" and one for "unique?". So here are all the headings I have currently:
A -- ID
B -- CollectionDate
C -- DOB
D -- MOTHER ID
E -- Sex
F -- # older siblings
G -- unique?
In G2, I added the following formula:
=IF(A2=A1,0,1)
And dragged down. As long as the data is sorted by ID, this will only display "1" once for each unique person.
In F2, I added the following formula:
=COUNTIFS(G:G,"=1",D:D,"="&D2,C:C,"<"&C2)
And dragged down. It seemed to work correctly for the sample data you provided.
The stipulations are:
You would need the two columns.
The data would need to be sorted by ID
I hope this helps.
You need a formula like this (for example, for row 2):
=COUNTIFS($A:$A,"<>"&$A2,$E:$E,$E2,$D:$D,$D2,$C:$C,"<"&$C2)
Assuming E:E is column for sex, D:D is column for mother ID and C:C is column for DOB.
Write this formula in H2 cell for example and drag it down.

How to automatically insert a blank row after a group of data

I have created a sample table below that is similar-enough to my table in excel that it should serve to illustrate the question. I want to simply add a row after each distinct datum in column1 (simplest way, using excel, thanks).
_
CURRENT TABLE:
column1 | column2 | column3
----------------------------------
A | small | blue
A | small | orange
A | small | yellow
B | med | yellow
B | med | blue
C | large | green
D | large | green
D | small | pink
_
DESIRED TABLE
Note: the blank row after each distinct column1
column1 | column2 | column3
----------------------------------
A | small | blue
A | small | orange
A | small | yellow
B | med | yellow
B | med | blue
C | large | green
D | large | green
D | small | pink
This does exactly what you are asking, checks the rows, and inserts a blank empty row at each change in column A:
sub AddBlankRows()
'
dim iRow as integer, iCol as integer
dim oRng as range
set oRng=range("a1")
irow=oRng.row
icol=oRng.column
do
'
if cells(irow+1, iCol)<>cells(irow,iCol) then
cells(irow+1,iCol).entirerow.insert shift:=xldown
irow=irow+2
else
irow=irow+1
end if
'
loop while not cells (irow,iCol).text=""
'
end sub
I hope that gets you started, let us know!
Philip
Select your array, including column labels, DATA > Outline -Subtotal, At each change in: column1, Use function: Count, Add subtotal to: column3, check Replace current subtotals and Summary below data, OK.
Filter and select for Column1, Text Filters, Contains..., Count, OK. Select all visible apart from the labels and delete contents. Remove filter and, if desired, ungroup rows.
This won't work if the data is not sequential (1 2 3 4 but 5 7 3 1 5) as in that case you can't sort it.
Here is how I solve that issue for me:
Column A initial data that needs to contain 5 rows between each number -
5
4
6
8
9
Column B -
1
2
3
4
5
(final number represents the number of empty rows that you need to be between numbers in column A) copy-paste 1-5 in column B as long as you have numbers in column A.
Jump to D column, in D1 type 1. In D2 type this formula - =IF(B2=1,1+D1,D1)
Drag it to the same length as column B.
Back to Column C - at C1 cell type this formula - =IF(B1=1,INDIRECT("a"&(D1)),""). Drag it down and we done. Now in column C we have same sequence of numbers as in column A distributed separately by 4 rows.
Figured it out.
Step 1
Put a new column to the left of column1 and copy+paste the following formula
=B2=B3
=B3=B4
=B4=B5
... all the way to the bottom (assume column B here is column1 in the original question).
This formula evaluates whether or not the next row is a new value in column1. Deopending on the result, you will have TRUE or FALSE. Copy and Paste these results as values and then swap "FALSE" for nil and "TRUE" for 0.5
Step 2
Then add that column full of only 0.5's to the column1 which will yield the following table:
newcolumn0 | column1 ("B") | column2 | column3
-----------------------------------------------------
| 1 | small | blue
| 1 | small | orange
1.5 | 1 | small | yellow
| 2 | med | yellow
2.5 | 2 | med | blue
3.5 | 3 | large | green
| 4 | large | green
4.5 | 4 | small | pink
Step 3
Lastly, copy and paste the values from newcolumn0 right below the values in column1 and then sort the table by column1 and you should have a blank row in between each distinct whole number in column1, with the table something like this:
newcolumn0 | column1 ("B") | column2 | column3
---------------------------------------------------------------
| 1 | small | blue
| 1 | small | orange
1.5 | 1.5 | |
| 1 | small | yellow
| 2 | med | yellow
| 2 | med | blue
2.5 | 2.5 | |
| 3 | large | green
3.5 | 3.5 | |
| 4 | large | green
| 4 | small | pink
4.5 | 4.5 | |
Alternative Solutions (still no VBA)
Put a value of 1 Column 1, Row 2 (assume this is A2)
Put this formula in A3 =IF(B3=B2,A2,A2+1) and copy+paste this formula for the rest of column 2
Then copy and paste all the values from column 1 into a new temp excel sheet, remove duplicates, then add 0.5 to all numbers, then paste these values below the values in original spreadsheet below the data in column 1, paste all data in column as values and then sort by that column, delete the temp excel sheet
Just an idea, if you know the categories, as small, medium, and large mentioned above...
At the bottom of the sheet, make 3 rows that only say small, medium, and large, change the font to white, and then sort so that it alphabetizes, placing a blank row between each section.
Insert a column at the left of the table 'Control'
Number the data as 1 to 1000 (assuming there are 1000 rows)
Copy the key field to another sheet and remove duplicates
Copy the unique row items to the main sheet, after 1000th record
In the 'Control' column, add number 1001 to all unique records
Sort the data (including the added records), first on key field and then on 'Control'
A blank line (with data in key field and 'Control') is added
I have a large file in excel dealing with purchase and sale of mutual fund units. Number of rows in a worksheet exceeds 4000. I have no experience with VBA and would like to work with basic excel. Taking the cue from the solutions suggested above, I tried to solve the problem ( to insert blank rows automatically) in the following manner:
I sorted my file according to control fields
I added a column to the file
I used the "IF" function to determine when there is a change in the control data .
If there is a change the result will indicate "yes", otherwise "no"
Then I filtered the data to group all "yes" items
I copied mutual fund names, folio number etc (no financial data)
Then I removed the filter and sorted the file again. The result is a row added at the desired place. (It is not entirely a blank row, because if it is fully blank, sorting will not place the row at the desired place.)
After sorting, you can easily delete all values to get a completely blank row.
This method also may be tried by the readers.

Resources