I have a table in excel of the following format:
╔════════╦════════╦════════╦════════╦═════════╗
║ Field1 ║ Field2 ║ Field3 ║ ... ║ Field10 ║
╠════════╬════════╬════════╬════════╬═════════╣
║ no ║ no ║ no ║ ... ║ no ║
║ no ║ yes ║ no ║ ... ║ no ║
║ yes ║ yes ║ yes ║ ... ║ yes ║
║ yes ║ yes ║ no ║ ... ║ yes ║
║ . ║ . ║ . ║ ... ║ . ║
║ . ║ . ║ . ║ ... ║ . ║
║ . ║ . ║ . ║ ... ║ . ║
╚════════╩════════╩════════╩════════╩═════════╝
Where as you can see, each field can have any combination of yes's and no's
I am trying to create a single field which is based on the data format previously shown. This field will contain the name(s) of fields which contained a "yes". If a "yes" exists in more than one Field, then it should list those fields separated by a comma.
Here is an example of what that desired field might look like:
╔════════╦════════╦════════╦════════╦═════════╦══════════════════════════╗
║ Field1 ║ Field2 ║ Field3 ║ ... ║ Field10 ║ NewField ║
╠════════╬════════╬════════╬════════╬═════════╬══════════════════════════╣
║ no ║ no ║ no ║ ... ║ no ║ ║
║ no ║ yes ║ no ║ ... ║ no ║ Field2 ║
║ yes ║ yes ║ yes ║ ... ║ yes ║ Field1, ..., Field10 ║
║ yes ║ yes ║ no ║ ... ║ yes ║ Field1, Field2, Field10 ║
║ . ║ . ║ . ║ ... ║ . ║ ... ║
║ . ║ . ║ . ║ ... ║ . ║ ... ║
║ . ║ . ║ . ║ ... ║ . ║ ... ║
╚════════╩════════╩════════╩════════╩═════════╩══════════════════════════╝
I am trying to achieve this with an excel formula, but so far the only solution apparent to me involves including every possible permutation in the excel formula. Of course, this is inefficient and time-consuming to create and to make changes to. Is there any way to achieve this result efficiently?
If you do not have Office 365 Excel then here is a Custom UDF to do what you want:
Function JoinField(ttl As Range, srchrng As Range, crit As Variant, Optional sep As String = ",") As String
Dim ttlArr() As Variant
Dim srchrngArr() As Variant
Dim i&,j&
ttlArr = ttl.Value
srchrngArr = srchrng.Value
If UBound(ttlArr, 1) <> UBound(srchrngArr, 1) Or UBound(ttlArr, 2) <> UBound(srchrngArr, 2) Then Exit Function
For i = LBound(ttlArr, 1) To UBound(ttlArr, 1)
For j = LBound(ttlArr, 2) To UBound(ttlArr, 2)
If srchrngArr(i, j) = crit Then
JoinField = JoinField & ttlArr(i, j) & sep
End If
Next j
Next i
JoinField = Left(JoinField, Len(JoinField) - Len(sep))
End Function
Put this in a module attached to the workbook. DO NOT put it in the worksheet code or ThisWorkbook code.
It is then called like a normal function:
=JoinField($A$1:$J$1,$A2:$J2,"Yes",",")
Where the first criteria is the names to concatenate. The second is the Range that has the criteria. The third is the Criteria to find. The forth is optional separation character. Default is ,.
If you have the latest Office 365 Excel you can use this formula as an array.
=TEXTJOIN(",",TRUE,IF($A2:$J2 = "Yes", $A$1:$J$1,""))
Being an array it needs to be confirmed with Ctrl-Shift-Enter when exiting edit mode instead of Enter. If done correctly Excel will put {} around the formula.
As a demonstration of how tedious and verbose the FORMULA would get using multiple IF formulas, you could wind up with a formula looking like the following:
=IF(LEFT(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&IF(J2="yes",", "&J$1,""),1)=",",SUBSTITUTE(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&IF(J2="yes",", "&J$1,""),", ","",1),IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&IF(J2="yes",", "&J$1,""))
The above formula assumed row 1 as your field headers, and your first field starting column A. The above formula would be placed in K2 and copied down.
Now if for some reason you are not allowed to use VBA or save your your worksheet in and .XLSM format, then you would need something like that hideous formula above.
With out the scroll bar, the formula looks more like:
=IF(LEFT(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&
IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&
IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&
IF(J2="yes",", "&J$1,""),1)=",",
SUBSTITUTE(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&
IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&
IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&
IF(J2="yes",", "&J$1,""),", ","",1),
IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&
IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&
IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&
IF(J2="yes",", "&J$1,""))
Proof of ugliness at work
A major down side of this is that if you needed to add another field the editing of the formula would be a royal pain in the butt!
If theres only 10 Columns you could go with sth. like this
=IF(A2=1;INDIRECT("A1";TRUE);"") & ", " & IF(B2=1;INDIRECT("B1";TRUE);"") & ", " & ....
In this Example a just added 0, 1 as values not the yes/no.
I have a huge excel sheet that looks like this:
╔══════╦══════╦═════╗
║ A ║ B ║ C ║
╠══════╬══════╬═════╣
║ Jack ║ 2001 ║ 1,5 ║
║ Jack ║ 2002 ║ 2,0 ║
║ Jack ║ 2003 ║ 1,0 ║
║ Jack ║ 3001 ║ 3,5 ║
║ Jack ║ 3002 ║ 4,0 ║
║ Jack ║ 3003 ║ 1,0 ║
║ Jill ║ 2001 ║ 3,0 ║
║ Jill ║ 2002 ║ 5,0 ║
║ Jill ║ 2003 ║ 2,0 ║
║ Jill ║ 3001 ║ 0,5 ║
║ Jill ║ 3002 ║ 6,0 ║
║ Jill ║ 3003 ║ 2,5 ║
╚══════╩══════╩═════╝
Column B contains many different numbers, but they all begin with digits 2, 3 or 8. The numbers in column B are always be 4 digits long; I'm only interested in checking the first digit.
I need to add together the values of column C, where the first digit of the corresponding cell in column B is either 2*, 3* or 8*. What I need is to create a formula that does this (Ruby-esque pseudocode):
sum = 0
spreadsheet_rows.each do |row|
if row.a == "Jack" and row.b == "2*" # Note the second wildcard condition.
sum += row.c
end
end
puts sum # Should print 4,5 in this example.
I'm trying to use the following formula in Excel to accomplish this:
=SUMIFS($C:$C; $A:$A; "Jack"; $B:$B; "=2*")
I know that Excel does not support wildcard conditions for numbers, however, I have formatted column B as type "Text" in Excel, so I thought it would be treated as such, but it appears that it is still treated as an int.
Is there a different way of applying a wildcard condition in =SUMIFS for number values in Excel? Perhaps there's a way to somehow "cast" the integers to strings in the formula? I haven't found a way to do it (yet).
I'm using Excel for Mac 2011.
I'd go for the less readable, but more powerful SUMPRODUCT:
=SUMPRODUCT(($A:$A="Jack") * (LEFT($B:$B;1)="2") * ($C:$C))
which will generate boolean arrays for each of the conditions (first and second brace part) which it will multiply with the third one (your numbers).
EDIT:
As noted in comments, #VALUE errors can appear if any value in column C cannot be converted to a number. To avoid that, you could use the syntax suggested by barry houdini
=SUMPRODUCT(($A:$A="Jack") * (LEFT($B:$B;1)="2"); $C:$C)
and let SUMPRODUCT skip over non-numbers.
This works for me:
=SUM((A1:A12=F2)*(LEFT(B1:B12)=""&F3)*C1:C12)
entered as an array formula with CtrlShiftEnter
You ask how to cast numbers to strings; concatenating an empty string and a number ""&F3 is one way to do that.
{=SUM((A1:A12=F2)*(LEFT(B1:B12)=""&F3)*C1:C12)}
No need for an arrays as shown above just type following formula which gives equal results to above Arrays formula
=SUMPRODUCT((A1:A12=F2)*(LEFT(B1:B12)=F3&"")*C1:C12)
Remember difference [Arrays=""&F3] vs [SUMPRODUCT= F3&""]
I shall be bring to your kind I am just very very very happy to see your work and the way you authoritatively asked question as as
"I know that Excel does not support wildcard conditions for numbers, however, I have
formatted column B as type "Text" in Excel, so I thought it would be treated as
such, but it appears that it is still treated as an int."
SUMPRODUCT only not work when we have to get output in TEXT
Hoping your verification about all above and kindly highlight at prominent place