如果下面行中的值非空,则连接列标题 [英] Concatenate column headers if value in rows below is non-blank
问题描述
我有以下格式的数据表.
I have table with data in the format below.
我希望完成的表格中的空白列像下面突出显示的那样填充.
I want the finished table to have the blank column to be populated like the highlighted one below.
所以,我需要一个公式或 VBA 来返回并连接每一行中非空白单元格的标题.
So, I need a formula or VBA that will return and concatenate the headers of non-blank cells in each row.
推荐答案
使用¹以下作为数组公式.
=TEXTJOIN("-->", TRUE, IF(LEN(C3:I3), C$2:I$2, ""))
Pre-Excel 2016 版本
虽然您可以将一系列 IF 语句串在一起,但更简洁的替代方法可能是编写用户定义的函数(又名 UDF).
While you could just string together a series of IF statements, a cleaner alternate might be to write a user defined function (aka UDF).
在标准 VBA 模块代码表中:
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
¹ TEXTJOIN 随 Excel 2016 引入以下版本:Excel for Android、Excel Mobile、Excel 2016 with Office 365、Excel 2016 for Mac、Excel Online、Excel for iPad、Excel for iPhone 和 Excel for Android平板电脑.
¹ 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.
这篇关于如果下面行中的值非空,则连接列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!