简体   繁体   English

如何根据条件连接多个单元格中的值?

[英]How to concatenate values in multiple cells based on a condition?

I have a need to search a row of cells, and for every cell that contains a specific value, return the value from the cell above. 我需要搜索一行单元格,并且对于包含特定值的每个单元格,从上面的单元格返回值。

For example, consider the following 例如,请考虑以下内容

+---+--------+--------+--------+--------+--------+----------+
|   |   A    |   B    |   C    |   D    |   E    |     F    |
+---+--------+--------+--------+--------+--------+----------+
| 1 |   UK   |   DE   |   FR   |   HK   |   TW   |          |
+---+--------+--------+--------+--------+--------+----------+
| 2 |   YES  |        |   YES  |   YES  |        |          |
+---+--------+--------+--------+--------+--------+----------+
| 3 |        |   YES  |        |   YES  |   YES  |          |
+---+--------+--------+--------+--------+--------+----------+
| 4 |   YES  |        |        |   YES  |        |          |
+---+--------+--------+--------+--------+--------+----------+

So I want to insert a formula into cells F2, F3 and F4 which will give the following results 所以我想在细胞F2,F3和F4中插入一个公式,这将给出以下结果

F2 = UK,FR,HK
F3 = DE,HK,TW
F4 = UK,HK

Can this be done? 可以这样做吗?

Thanks 谢谢

I have found a simple, scalable solution that uses an array formula to concatenate multiple cells that satisfy a certain condition. 我找到了一个简单,可扩展的解决方案,它使用数组公式来连接满足特定条件的多个单元格。

Applied to your example, paste into cell F2: 应用于您的示例,粘贴到单元格F2:

=TEXTJOIN(",", TRUE, IF(B3:F3 = "YES", B$2:F$2, ""))

and hit ctrl+shift+enter to enter as an array formula , and copy over cells F3--F4. 然后点击ctrl + shift + enter作为数组公式输入,并复制单元格F3-F4。

The reason why this works is left as an exercise to the reader. 其作用的原因留给读者练习。 It's fairly clear, but I prefer "magic". 这很清楚,但我更喜欢“魔术”。

I hope this helps anyone with a similar problem. 我希望这可以帮助任何有类似问题的人。

In F2 copy-paste this formula: 在F2中复制粘贴这个公式:

=CONCATENATE(IF($A2="YES",A$1&",",),IF($B2="YES",B$1&",",),IF($C2="YES",C$1&",",),IF($D2="YES",D$1&",",),IF($E2="YES",E$1&",",))

and drag down against the column. 并向下拖动列。

Explanation: 说明:

IF($A2="YES",A$1&",",)
IF($B2="YES",B$1&",",)
IF($C2="YES",C$1&",",)
IF($D2="YES",D$1&",",)
IF($E2="YES",E$1&",",)

The above code has been rewritten 5 times and the column name has been changed. 上面的代码已被重写5次,列名已被更改。 It checks whether the cell in the current row has a 'YES'. 它检查当前行中的单元格是否为“是”。 If it does then it'll enter the header of the column which is 'A$1' . 如果是,那么它将输入列的标题'A$1' Notice that $1 is absolute reference to the first row ie the header. 请注意,$ 1是第一行的绝对引用,即标题。

In the end, I have encapsulated all the five IF statements using CONCATENATE statement. 最后,我使用CONCATENATE语句封装了所有五个IF语句。

Hope this helps. 希望这可以帮助。

Write your own UDF 编写自己的UDF

Original solution. 原始解决方案

Excerpt from the article 摘自文章

  1. Open VBA Editor by clicking Visual Basic on Developer tab or using Alt + F11 combination 单击“ 开发人员”选项卡上的“ Visual Basic ”或使用Alt + F11组合打开VBA编辑器
  2. Create new module by right clicking Microsoft Excel Objects in the top left corner and choosing Insert->Module from the context menu. 通过右键单击左上角的Microsoft Excel对象并从上下文菜单中选择“ 插入” - >“模块”来创建新模块。
  3. Insert the following code 插入以下代码

UDF: UDF:

Function ConcatenateIf(CriteriaRange As Range, _
                       Condition 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 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

Later on you can use it, if you enable macro in your workbook. 稍后,如果在工作簿中启用宏,则可以使用它。
In your specific example write the following formula into F2 cell and copy over needed range. 在您的具体示例中,将以下公式写入F2单元格并复制所需范围。

=ConcatenateIf($A2:$E2,"YES",$A$1:$E$1,",")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM