在 Excel 中计算笛卡尔积后的非空值

在 Excel 中计算笛卡尔积后的非空值

我希望我的描述准确无误;欢迎改进。
我在 Excel 中有 2 个表。表 1 中有 2 列,A 和 B。它可以是多对多关系。
表 2 中有 2 列,A 和 B。这些是不同的条目。
表 1-ColumnB 和表 2-ColumnA 是相同的值 - 如果您愿意的话,可以称为外键。

我在 Result-List 中有一个来自 Table1-ColumnA 的不同值列表。我需要知道对于此列表中的每个项目,在外键连接之后,Table2-Column2 中的所有关联值是否为空。(我不需要知道它有什么内容,只要知道它是否有内容即可。)

所有这些都需要在 Excel 中的一个单元格中完成。

表格和结果

答案1

如果有人知道如何使用数组公式获得您想要的结果,请告诉我们。我认为这是不可能的……因此我使用 VBA。这是我的编码解决方案(UDF):

Option Explicit
Function Results(cell As Range, table_1 As Range, table_2 As Range) As String
Dim aTable1() As Variant
Dim aTable2() As Variant
aTable1 = table_1.Value
aTable2 = table_2.Value
Dim i As Long, j As Long 'counters

For i = 1 To UBound(aTable1, 1)
    If aTable1(i, 1) = cell.Value Then
        For j = 1 To UBound(aTable2, 1)
            If aTable1(i, 2) = aTable2(j, 1) Then
                If Not IsEmpty(aTable2(j, 2)) Then
                    Results = Results & aTable2(j, 1) & " has content, "
                Else
                    Results = Results & aTable2(j, 1) & " has no content, "
                End If
            End If
        Next
        If Results = vbNullString Then
            Results = aTable1(i, 2) & " NOT FOUND"
            GoTo Ending
        End If
    End If
Next
Results = Left(Results, Len(Results) - 2)
If (Len(Results) - Len(Replace(Results, "has", ""))) / 3 = _
(Len(Results) - Len(Replace(Results, "no", ""))) / 2 Then
    Results = "BLANK - " & Results
End If

Ending:
End Function

这是您得到的结果: 自定义函数

您也可以使用通常的范围引用,即在中I3我也可以插入这个公式:=Results(H3,B$3:C$13,E$3:F$10)

将代码粘贴到 VBA 编辑器 (Alt+F11),在新插入的“Module1”中(而不是在“ThisWorkbook”或“Sheet”模块中)

相关内容