Concatenate column headers if value in rows below is non-blank

后端 未结 1 504
北荒
北荒 2020-11-30 14:52

I have table with data in the format below.

Data

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

相关标签:
1条回答
  • 2020-11-30 15:18

    Use¹ the following as an array formula.

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

    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
    


    ¹ 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.

    0 讨论(0)
提交回复
热议问题