3

I have table with data in the format below.

Data
enter image description here

I want the finished table to have the blank column to be populated like the highlighted one below.

Finished Table
enter image description here

So, I need a formula or VBA that will return and concatenate the headers of non-blank cells in each row.

Elissa
  • 49
  • 1
  • 5

1 Answers1

8

Use¹ the following as an array formula.

=TEXTJOIN("-->", TRUE, IF(LEN(C3:I3), C$2:I$2, ""))

enter image description here

Pre-Excel 2016 versions

While you could just string together a series of IF statements, a cleaner alternate might be to write a user defined function (aka UDF).

In a standard VBA module code sheet:

Function udf_Stitch_Together(r As Range, _
                             h As Range, _
                             Optional d As String = "-->", _
                             Optional blnks As Boolean = False) As String
    Dim s As String, c As Long
    For c = 1 To r.Cells.Count
        If CBool(Len(r.Cells(c).Text)) Then _
            s = s & IIf(Len(s), d, vbNullString) & h.Cells(c).Text
    Next c
    udf_Stitch_Together = s
End Function

enter image description here


¹ The TEXTJOIN was introduced with Excel 2016 in the following versions:Excel for Android phones, Excel Mobile, Excel 2016 with Office 365, Excel 2016 for Mac, Excel Online, Excel for iPad, Excel for iPhone and Excel for Android tablet.

  • 1
    Clever! I was playing with `TextJoin()` but couldn't find a nice way to only fill cells with a value. – BruceWayne Mar 29 '17 at 22:20
  • I'm using Excel 2010 and it's not recognizing the TEXTJOIN formula. But looks like that is exactly what I want to do. Any similar formula for my version of excel? – Elissa Mar 29 '17 at 22:30
  • That would depend on how many **actual** columns you want to examine. Seven columns is not out of scope for even seven nested IF conditions but there are shorter formulas. –  Mar 29 '17 at 22:37
  • There are 10 columns – Elissa Mar 29 '17 at 22:39