excel公式如何返回满足条件的数组

vlookup 只能返回第一个数,有办法可以返回满足这个条件的全部单元格的数组吗?vlookup不行,有其他函数吗?
例如:
c1 c2
1 a
2 c
1 d

求:c1为1的c2的值,返回数组{"a","d"}

写 VBA 函数

SplicingRange(Arr As Range, n As Integer),第一个参数是选定区域,第二个参数是 你想查询的哪个值

img


结果

img



Function SplicingRange(Arr As Range, n As Integer)
    Dim x As Integer
     For i = 1 To Arr.Rows.Count
        If Arr.Cells(i, 1) = n Then
            If SplicingRange <> "" Then
               SplicingRange = SplicingRange & "," & Arr.Cells(i, 2)
            Else
                SplicingRange = Arr.Cells(i, 2)
            End If
        End If
     Next
      
End Function


插入开发工具,并启用宏

img

改为返回 数组

img



Function SplicingRange(Arr As Range, n As Integer)
    Dim x As Integer
     For i = 1 To Arr.Rows.Count
        If Arr.Cells(i, 1) = n Then
            If SplicingRange <> "" Then
               SplicingRange = SplicingRange & ",""" & Arr.Cells(i, 2) & """"
            Else
                SplicingRange = "{""" & Arr.Cells(i, 2) & """"
            End If
        End If
     Next
     SplicingRange = SplicingRange & "}"
      
End Function

C1列加个筛选不就好了
自定义公式如下:
文件-》选项-》自定义功能区-》勾选开发工具,然后在开发工具菜单,点击Visual Basic
或者Alt + F11
按图所示操作:

img

img


在新增模块里添加自定义函数

Function TextMatchJoin(Delimiter As String, joinRange As Range, ParamArray Rng() As Variant)
    Dim cell As Range, i As Integer, j As Integer, item As Variant, totalCount As Integer, startRow As Integer
    Dim match As Boolean
    
    totalCount = ActiveSheet.UsedRange.Rows.Count
    If totalCount > joinRange.Count Then totalCount = joinRange.Count
    startRow = joinRange.Row
    
    For i = 0 To totalCount - 1
        item = joinRange.Cells(i + 1)
        match = True
        For j = 0 To UBound(Rng) Step 2
            If Not IsMissing(Rng(j)) And j + 1 <= UBound(Rng) Then
                If TypeName(Rng(j)) = "Range" Then
                    If Rng(j).Cells(i + 1) <> Rng(j + 1) Then
                        match = False
                    End If
                End If
            End If
        Next j
        If match Then
            TextMatchJoin = TextMatchJoin & item & Delimiter
        End If
    Next i
    If Len(TextMatchJoin) > 2 Then TextMatchJoin = Left(TextMatchJoin, Len(TextMatchJoin) - Len(Delimiter))
End Function

img

公式我这里给你做了个扩展
如果只想选择一列,可以改成如下:
= "{" & TextMatchJoin(",",C:C,A:A,F2) & "}"
公式格式定义如下
TextMatchJoin(分隔符,匹配列,条件1列,条件1值,条件2列,条件2值,条件3列,条件3值...)

FILTER函数可以,用法自己看语法

img

参考这个链接吧,http://www.ittribalwo.com/article/4865.html

纯excel公式法:
如下是显示公式的状态

img

如下是实际显示状态

img

可以使用Java实现吗?