Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to do some excel spreadsheet design that is a bit over my head. Say you have columns A, B, C, and D. In columns B thru D, you have X's marked in the cells. The columns are headed with the letters B,C,and D. In column A I am trying to incorporate the list of X's but instead want to list the headers.
So say row 2 has an X in both column B and column D. How would I make the A2 output read "B,D"? I basically need to list all the outputs like so.

If you really only have three columns of data, then you can do it with a single brute force nested IF statement. The problem is that any more than 3 columns would require a formula with more than 7 nested IF's which is not as straight forward. (Excel has a limit of 7 nested IF's in a single formula.)
If your actual requirements are more complicated than your example, post some more details and we'll see what we can do.
Here's an example of a nested IF that should work with your example:
=IF(AND(B2="X",C2="X",D2="X"),$B$1&","&$C$1&","&$D$1,IF(AND(C2="X",D2="X"),$C$1&","&$D$1,IF(AND(B2="X",C2="X"),$B$1&","&$C$1,IF(AND(B2="X",D2="X"),$B$1&","&$D$1,IF(B2="X",$B$1,IF(C2="X",$C$1,IF(D2="X",$D$1,"")))))))

You're formula worked perectly for the example, thank you. I guess I should have thought about the difference between my example and actual application a little more though. The actual spreadsheet I'm working on has 11 columns, which increases the amount of combinations exponentially. Do you or anybody else know of a non-brute force method that would allow me to do this? I did one with the CANCATENATE formula with embedded IF statements, but the output was formatted poorly. Let me know and thanks again!

I'd be interested in seeing the formula you tried. Maybe I can clean up the output for you.
In the meantime, this Worksheet_Change macro seems to work for your 11 columns. The code assumes your column headings are in B1:L1 and that you want the output in column A of the same row as a given set of X's. As you enter or delete X's in columns B through L, the results in column A should update.
To use it, right click the sheet tab, choose View Code and paste the code into the window that opens. Then go back to your sheet and start entering or deleting X's as needed.
If you already have a sheet full of X's, the code can easily be modified to read the existing X's and fill in column A.
Let me know if you need further assistance...
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column > 1 Or Target.Column < 12 Then
RowNum = Target.Row
For ColNum = 2 To 12
If UCase(Cells(RowNum, ColNum)) = "X" Then
Pass = Pass + 1
If Pass = 1 Then
FoundX = Cells(1, ColNum)
Else: FoundX = FoundX & "," & Cells(1, ColNum)
End If
End If
Next
Cells(RowNum, 1) = FoundX
End If
End Sub

This seems to work...
It's really long for 2 reasons:
1 - With just the Concatenated IFs, it always returns a trailing comma. I eliminate that by using the LEFT and LEN functions to subtract 1 character from the result.
2 - Due to the use of the LEFT function, the formula will return a #VALUE error if there are no X's in any column. I start with a LEN function to check and see if the Concatenated IFs actually return something before proceeding. If the length of the Concatenated IFs is 0, then the formula returns nothing.
Anyway...here it is:
=IF(LEN(IF(B2="x",B$1&",","")&IF(C2="x",C$1&",","")&IF(D2="x",D$1&",","")&IF(E2="x",E$1&",","")&IF(F2="x",F$1&",","")&IF(G2="x",G$1&",","")&IF(H2="x",H$1&",","")&IF(I2="x",I$1&",","")&IF(J2="x",J$1&",","")&IF(K2="x",K$1&",","")&IF(L2="x",L$1&",",""))>0,LEFT(IF(B2="x",B$1&",","")&IF(C2="x",C$1&",","")&IF(D2="x",D$1&",","")&IF(E2="x",E$1&",","")&IF(F2="x",F$1&",","")&IF(G2="x",G$1&",","")&IF(H2="x",H$1&",","")&IF(I2="x",I$1&",","")&IF(J2="x",J$1&",","")&IF(K2="x",K$1&",","")&IF(L2="x",L$1&",",""),LEN(IF(B2="x",B$1&",","")&IF(C2="x",C$1&",","")&IF(D2="x",D$1&",","")&IF(E2="x",E$1&",","")&IF(F2="x",F$1&",","")&IF(G2="x",G$1&",","")&IF(H2="x",H$1&",","")&IF(I2="x",I$1&",","")&IF(J2="x",J$1&",","")&IF(K2="x",K$1&",","")&IF(L2="x",L$1&",",""))-1),"")

And finally, with help of a few friends, we came up with this.
The idea is to:
1 - Put an extra comma at the front, so it is easier to strip out with the MID function, and
2 - Add a space at the end so if there are no X's we won't get an error.The TRIM function strips off the extra space, leaving you with just the column headings and commas where they belong.
Enjoy!
=TRIM(MID(IF(B2="X",","&B$1,"")&IF(C2="X",","&C$1,"")&IF(D2="X",","&D$1,"")&IF(E2="X",","&E$1,"")&IF(F2="X",","&F$1,"")&IF(G2="X",","&G$1,"")&IF(H2="X",","&H$1,"")&IF(I2="X",","&I$1,"")&IF(J2="X",","&J$1,"")&IF(K2="X",","&K$1,"")&IF(L2="X",","&L$1,"")&" ",2,999))

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |