根据两个条件连接列中的某些单元格
Concatenate certain cells from column based on two conditions
我有一个 Excel 工作表,其中包含不同的足球队及其小队,以及有关球员个人的信息。我想做的是,从我的 table 连接某些单元格并将该数据放入一个更简单的单元格中 table 以供其他用户查看,例如,显示哪些球员受伤目前在所有球队。我来解释一下:
F_Team | Player | Injured
Liverpool Coutinho 0
Liverpool Benteke 1
Liverpool Sturridge 1
Man U Rooney 1
Chelsea Sterling 0
所以在我的另一个 table 中看起来像这样
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:$D000, $L:$L000, Z2, 1, $E:$E000)
D 列是 F_Team
E列是玩家
L柱受伤
Z 列是 F_Team 与 D 列
匹配的内容
If CriteriaRange.Cells(i).Value = Condition And criteriarange2 =
condition2 Then
应该是:
If CriteriaRange.Cells(i).Value = Condition And criteriarange2.Cells(i).Value = condition2 Then
p.s:在我的测试中,使用原始代码我没有得到#NAME,而是所有玩家的完整列表。那是因为 On Error Resume Next
语句。我强烈建议省略此语句:因为这是一个 UDF,如果用户输入错误,让 Excel 以自己的方式处理错误(显示 #VALUE),而不是显示任何错误的数据。
我有一个 Excel 工作表,其中包含不同的足球队及其小队,以及有关球员个人的信息。我想做的是,从我的 table 连接某些单元格并将该数据放入一个更简单的单元格中 table 以供其他用户查看,例如,显示哪些球员受伤目前在所有球队。我来解释一下:
F_Team | Player | Injured
Liverpool Coutinho 0
Liverpool Benteke 1
Liverpool Sturridge 1
Man U Rooney 1
Chelsea Sterling 0
所以在我的另一个 table 中看起来像这样
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:$D000, $L:$L000, Z2, 1, $E:$E000)
D 列是 F_Team E列是玩家 L柱受伤 Z 列是 F_Team 与 D 列
匹配的内容If CriteriaRange.Cells(i).Value = Condition And criteriarange2 = condition2 Then
应该是:
If CriteriaRange.Cells(i).Value = Condition And criteriarange2.Cells(i).Value = condition2 Then
p.s:在我的测试中,使用原始代码我没有得到#NAME,而是所有玩家的完整列表。那是因为 On Error Resume Next
语句。我强烈建议省略此语句:因为这是一个 UDF,如果用户输入错误,让 Excel 以自己的方式处理错误(显示 #VALUE),而不是显示任何错误的数据。