如何在根据相同标题列中的单元格值排序的单元格中输入条件标题列表?

时间:2019-05-16 19:43:02

标签: excel sorting excel-formula conditional

在MS Excel中,我试图根据标头是否具有值(大于0)在一个单元格中提供标头列表。我希望也按最大的顺序在此单元格中对列表进行排序。我该怎么办?

我根本没有学习过Visual Basic,并且只真正尝试过使用某些if语句。我有一个很大的桌子要整理,不想手工做。

我想要得到的东西:

         ---------  Header1   Header2   Header3   List
                    Apple     Orange    Pear  

Row 1 -     Ryan    0         0         0         --
Row 2 -     Cindy   0         5         7         Pears, Oranges
Row 3 -     Tom                                   N/A
Row 4 -     Jess    3         6         2         Oranges, Apples, Pears

谢谢!

3 个答案:

答案 0 :(得分:0)

如果您拥有Office 365 Excel,则可以使用以下数组公式:

=IFERROR(TEXTJOIN(",",TRUE,INDEX($B$1:$D$1,N(IF({1},MATCH(AGGREGATE(14,7,$B3:$D3/($B3:$D3>0),ROW($XFD$1:INDEX($XFD:$XFD,COUNTIF($B3:$D3,">0")))),$B3:$D3,0))))),IF(COUNT(B3:D3)>1,"---","N/A"))

要成为数组公式,退出编辑模式时必须使用Ctrl-Shift-Enter而不是Enter进行确认。

它按顺序构建标题数组,并将其返回给TEXTJOIN函数。需要注意的是,如果值之间有平局,则两者将返回相同的标头。例如,6,6,6将返回Apple,Apple,Apple

enter image description here


如果没有Office 365 Excel,则将此代码放在工作表所附的模块中,并使用上述公式:

Function TEXTJOIN(delim As String, skipblank As Boolean, arr)
    Dim d As Long
    Dim c As Long
    Dim arr2()
    Dim t As Long, y As Long
    t = -1
    y = -1
    If TypeName(arr) = "Range" Then
        arr2 = arr.Value
    Else
        arr2 = arr
    End If
    On Error Resume Next
    t = UBound(arr2, 2)
    y = UBound(arr2, 1)
    On Error GoTo 0

    If t >= 0 And y >= 0 Then
        For c = LBound(arr2, 1) To UBound(arr2, 1)
            For d = LBound(arr2, 1) To UBound(arr2, 2)
                If arr2(c, d) <> "" Or Not skipblank Then
                    TEXTJOIN = TEXTJOIN & arr2(c, d) & delim
                End If
            Next d
        Next c
    Else
        For c = LBound(arr2) To UBound(arr2)
            If arr2(c) <> "" Or Not skipblank Then
                TEXTJOIN = TEXTJOIN & arr2(c) & delim
            End If
        Next c
    End If
    TEXTJOIN = Left(TEXTJOIN, Len(TEXTJOIN) - Len(delim))
End Function

也可以一次创建一个刺痛的单词来完成此操作:

=MID(IF(B3>0,","&$B$1,"")&IF(C3>0,","&$C$1,"")&IF(D3>0,","&$D$1,""),2,999)

但是,如果有很多列,这将变得非常不规则:

enter image description here

答案 1 :(得分:0)

您可以使用旧的excel版本来执行此操作,但是为了在正确的位置显示正确的逗号,公式变得有点复杂。我将其分为两个公式以使其更清晰:

enter image description here

第一个公式是查找并写入标题(位于E列中):

 =+IF(COUNTIF(B6:D6,">"&0)>0,CONCATENATE(IF(B6>0,B$4&",",""),IF(C6>0,C$4&",",""),IF(D6>0,D$4,"")),"")

第二个是清除逗号并提供最终结果(位于F列中):

=+IF(RIGHT(E6,1)=",",LEFT(E6,LEN(E6)-1),E6)

您可以将两者合并,但是很难弄清楚公式的作用:

=+IF(RIGHT(IF(COUNTIF(B6:D6,">"&0)>0,CONCATENATE(IF(B6>0,B$4&",",""),IF(C6>0,C$4&",",""),IF(D6>0,D$4,"")),""),1)=",",LEFT(IF(COUNTIF(B6:D6,">"&0)>0,CONCATENATE(IF(B6>0,B$4&",",""),IF(C6>0,C$4&",",""),IF(D6>0,D$4,"")),""),LEN(IF(COUNTIF(B6:D6,">"&0)>0,CONCATENATE(IF(B6>0,B$4&",",""),IF(C6>0,C$4&",",""),IF(D6>0,D$4,"")),""))-1),IF(COUNTIF(B6:D6,">"&0)>0,CONCATENATE(IF(B6>0,B$4&",",""),IF(C6>0,C$4&",",""),IF(D6>0,D$4,"")),""))

答案 2 :(得分:-1)

欢迎堆栈溢出!

尝试签出TEXTJOIN将单元格合并到一个列表中。另外,请查看conditionally concatenating a range上的此链接,以获取实现的良好示例。祝你好运!