Return column header if X in cell

orjanmen

New Member
Joined
May 21, 2014
Messages
27
Office Version
  1. 365
Platform
  1. Windows
Hi,

Got this table:
1641811015764.png


...and would like this result:
1641811052958.png


If there is "X" in the cell, return value in 1:1. I have found multiple formulas to do so, but they only return first hit.

Anyone who can guide me in right direction on this one? Would appreciate it :)
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I'm not sure about a formula but this macro should do what you want. It assumes your data is in Sheet1 and the result will be displayed in Sheet2. It also assumes that you are using merged cells in row 1.
VBA Code:
Sub ReturnColHeader()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, Ary As Variant, r As Long, c As Long, lCol As Long
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    With srcWS
        LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1
        desWS.Range("A1").Resize(LastRow - 1).Value = srcWS.Range("A2").Resize(LastRow - 1).Value
        Ary = .Range("A1").Resize(LastRow, lCol).Value
        With CreateObject("scripting.dictionary")
            For r = 2 To UBound(Ary)
                For c = 2 To UBound(Ary, 2)
                    If Ary(r, c) = "x" Then
                        desWS.Cells(r - 1, desWS.Columns.Count).End(xlToLeft).Offset(0, 1) = Ary(1, c - 1)
                    End If
              Next c
           Next r
        End With
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Hi Orjanmen,
I am assuming there is a typo and the heading in F1 should actually be in G1, in which case this should work.

Orjanmen.xlsx
ABCDEFGHIJKLMNO
1m, 31.01t, 01.02w, 02.02th, 03.02f, 04.02sa, 05.02su, 06.02
2Jeff
3Stevenjxxx
4Austinnnxnnnnn
5Nicolex
6Emmannnxx
Sheet1


Cell Formulas
RangeFormula
A1:A5A1=Sheet1!A2
B1:E5B1=IFERROR(INDEX(Sheet1!$C$1:$O$1,AGGREGATE(15,6,COLUMN(Sheet1!$C$1:$O$1)-COLUMN(Sheet1!$B$1)/((Sheet1!$C2:$O2="x")),COUNTIF($A1:A1,"<>"))),"")
 
Upvote 0
Solution
Thank you for your replies. I went for the formula, but thanks for the VBA alternative.
 
Upvote 0
I'm not sure about a formula but this macro should do what you want. It assumes your data is in Sheet1 and the result will be displayed in Sheet2. It also assumes that you are using merged cells in row 1.
VBA Code:
Sub ReturnColHeader()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, Ary As Variant, r As Long, c As Long, lCol As Long
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    With srcWS
        LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1
        desWS.Range("A1").Resize(LastRow - 1).Value = srcWS.Range("A2").Resize(LastRow - 1).Value
        Ary = .Range("A1").Resize(LastRow, lCol).Value
        With CreateObject("scripting.dictionary")
            For r = 2 To UBound(Ary)
                For c = 2 To UBound(Ary, 2)
                    If Ary(r, c) = "x" Then
                        desWS.Cells(r - 1, desWS.Columns.Count).End(xlToLeft).Offset(0, 1) = Ary(1, c - 1)
                    End If
              Next c
           Next r
        End With
    End With
    Application.ScreenUpdating = True
End Sub
I landed on this question while searching for similar.
I have a question on mumps's VBA solution: why is a dictionary object required here? Looping through the array (Ary) could be regardless once the array (Ary) is allocated?
 
Upvote 0
You are absolutely correct. It is not necessary.
 
Upvote 0
You are very welcome and thank you for picking up on that. :)
 
Upvote 0

Forum statistics

Threads
1,226,063
Messages
6,188,655
Members
453,489
Latest member
jessrw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top