I need your help, I would like to retrieve the value of the column where the cell contains 1 here is an example:

Image

## A | B | C | D

## 1 | 0 | 0 |

## 0 | 1 | 1 |

## 1 | 1 | 0 |

The desired result:

## A | B | C | D

## 1 | 0 | 0 | A

## 0 | 1 | 1 | B,C

## 1 | 1 | 0 | A,B

Thank you !

Here is a VBA approach that works for any number of columns:

```
Function SelectedColumns(R As Range) As String
Dim i As Long, n As Long, count As Long
Dim cols As Variant
n = R.Columns.count
ReDim cols(1 To n)
For i = 1 To n
If R.Cells(1, i).Value = 1 Then
count = count + 1
cols(count) = Split(R.Cells(1, i).Address, "$")(1)
End If
Next i
If count > 0 Then
ReDim Preserve cols(1 To count)
SelectedColumns = Join(cols, ",")
End If
End Function
```

Put the above code in a standard code module. The, in your example, if you enter `=SelectedColumns(A1:C1)`

in cell `D1`

and copy down then it will work as expected.

### Answer：

Here is the TEXTJOIN formula that can be used in Office 365 Excel:

```
=TEXTJOIN(",",,IF(A1:C1=1,{"A","B","C"},""))
```

This is an array formula and must be confirmed with Ctrl-Shift-Enter instead if Enter When exiting edit mode.

For Earlier versions you can use an if for each and concatenate:

```
=MID(IF(A1=1,",A","") & IF(B1=1,",B","") & IF(C1=1,",C",""),2,100)
```

### Answer：

I am a little unclear of your formatting (how you want the answer etc), but perhaps this will help you out a bit.

I am assuming your headers are in cells A1 through C1, then your data in cells A2 through C4. Ane you want the data in Cells D2 to F4.

If you copy the following into cell D2 then use the drag function it will produce the result you are after.

=IF(A2=1,A$1,””)

Have a look at the screen capture where I have displayed the formulas then another with the result displayer.

Hope this helps — Best of Luck !!!

Tags: excelexcel