【问题标题】:Concatenate certain cells from column based on two conditions根据两个条件连接列中的某些单元格
【发布时间】:2015-11-01 12:31:53
【问题描述】:

我有一个包含不同足球队及其球队的 Excel 工作表,以及有关各个球员的信息。我想要做的是,从我的表格中,连接某些单元格并将该数据放入一个更简单的表格中的单元格中,以供其他用户查看,例如,显示当前所有球队中哪些球员受伤。我来解释一下:

F_Team     | Player   | Injured
Liverpool   Coutinho       0
Liverpool   Benteke        1
Liverpool   Sturridge      1
Man U       Rooney         1
Chelsea     Sterling       0

所以在我的另一个表中它看起来像这样

F_Team     | Players Injured
Liverpool   Benteke, Sturridge
Man U       Rooney

因此可以将数据分组到各个团队中,我只是试图正确连接它。

我曾尝试使用此 VBA,但返回的只是 #NAME?,我不知道为什么,也不知道我所做的是否正确。

Function ConcatenateIf(CriteriaRange As Range, criteriarange2 As Range, _
Condition As Variant, condition2 As Variant, ConcatenateRange As Range, _
Optional Separator As String = ",") As Variant 'Update 20150414

Dim xResult As String
On Error Resume Next
If CriteriaRange.Count <> ConcatenateRange.Count Then
ConcatenateIf = CVErr(xlErrRef)
Exit Function
End If

For i = 1 To CriteriaRange.Count
If CriteriaRange.Cells(i).Value = Condition And criteriarange2 = condition2   Then
    xResult = xResult & Separator & ConcatenateRange.Cells(i).Value
End If
Next i

If xResult <> "" Then
xResult = VBA.Mid(xResult, VBA.Len(Separator) + 1)
End If
ConcatenateIf = xResult
Exit Function
End Function

我使用的公式:

=CONCATENATEIF($D$2:$D$20000, $L$2:$L$20000, Z2, 1, $E$2:$E$20000)

D 列是 F_Team E列是玩家 L列受伤 Z 列是 F_Team 与 D 列匹配的内容

【问题讨论】:

    标签: excel vba conditional concatenation


    【解决方案1】:

    如果 CriteriaRange.Cells(i).Value = Condition And criteriarange2 = 条件2然后

    应该是:

    If CriteriaRange.Cells(i).Value = Condition And criteriarange2.Cells(i).Value = condition2 Then
    

    ps:在我的测试中,使用原始代码我没有得到#NAME,而是所有玩家的完整列表。那是因为On Error Resume Next 声明。我强烈建议省略此语句:因为这是一个 UDF,如果用户输入错误,让 Excel 以自己的方式处理错误(显示 #VALUE),而不是显示任何错误数据。

    【讨论】:

    • 谢谢。我现在已经开始工作了。谢谢
    猜你喜欢
    • 2020-03-20
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 2023-03-21
    相关资源
    最近更新 更多