如果它们在某个范围内,则连接单元格

时间:2017-08-21 21:07:21

标签: excel excel-formula

我正在寻找一个公式来将单元格值(例如A1:E1)连接到G1,如果它们在一定范围内,就像单元格值小于< 7,或单元格值介于8和15之间等。

我尝试创建这样的公式:= concatenate(if(A1:E1< 7,“”))这是不正确的。我也无法在互联网上找到类似的例子。

示例:

A1:1 | B1:2 | C1:3 | D1:5 | E1:50  

Concatenate if A1:E1 is smaller than 7 => Result :  G1: (1,2,3,5)
Concatenate if A1:E1 is between 1 and 7 => Result : G1: (2,3,5)
Concatenate if A1:E1 is bigger than 10 => Result :  G1: (50)

我怎样才能做到这一点?谢谢!

1 个答案:

答案 0 :(得分:0)

使用TEXTJOIN作为数组公式:

=TEXTJOIN(",",TRUE,IF(A1:E1<=7,A1:E1,""))
=TEXTJOIN(",",TRUE,IF((A1:E1<=7)*(A1:E1>1),A1:E1,""))
=TEXTJOIN(",",TRUE,IF((A1:E1>=10),A1:E1,""))

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

enter image description here

TEXTJOIN是在Office 365 Excel中引入的。如果你没有那么它将需要vba。将以下内容放在工作簿附带的模块中,并使用上述公式。

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