[英]Lookup multiple values and return a single string
I have found how to lookup multiple values and return them as a list (one value in one cell). 我发现了如何查找多个值并将它们作为列表返回(一个单元格中有一个值)。 But I need to display the found list in a single cell (as a string with break lines);
但是我需要在单个单元格中显示找到的列表(作为带有换行符的字符串); preferably with a semicolon after each value.
最好在每个值后加上分号。
For example: 例如:
Source list (one value - one cell): 源列表(一个值-一个单元格):
A
A
B
B
B
C
Result (displayed in a single cell): 结果(显示在单个单元格中):
A;
B;
C;
Thank you! 谢谢!
You'll need to extract unique values and then concatenate those values. 您需要提取唯一值,然后将这些值连接起来。
When a2:a7 = {a, a, b, b, b, c}
, type this at b2
and hit ctrl + shift + enter
. 当
a2:a7 = {a, a, b, b, b, c}
,在b2
处键入此内容,然后按ctrl + shift + enter
。
=IFERROR(INDEX($A$2:$A$7,MATCH(SUM(COUNTIF(B$1:B1,$A$2:$A$7)),COUNTIF($A$2:$A$7,"<"&$A$2:$A$7),0)),"")
Then copy b2
and paste it onto b3:b7
. 然后复制
b2
并将其粘贴到b3:b7
。 Now you have a list of unique values. 现在,您有了一个唯一值列表。 This method is from this answer .
这个方法就是从这个答案 。
Now that you have the list, you only have to join them. 现在您有了列表,您只需加入即可。 type this at
c2
. 在
c2
。
=IF(B2="", "", B2&";"&CHAR(10))
&IF(B3="", "", B3&";"&CHAR(10))
&IF(B4="", "", B4&";"&CHAR(10))
&IF(B5="", "", B5&";"&CHAR(10))
&IF(B6="", "", B6&";"&CHAR(10))
&IF(B7="", "", B7&";"&CHAR(10))
See the picture. 看图片。
I know this is ulgy. 我知道这很麻烦。 But there's no built-in formula in Excel.
但是Excel中没有内置公式。 Other workarounds without VBA are here and there , just in case.
没有VBA的其他解决方法在这里和那里 ,以防万一。
By the way, Google Spreadsheet provides all of these as built-in functions. 顺便说一下,Google Spreadsheet将所有这些功能都作为内置函数提供。 Just one line.
只需一行。
=join(";"&char(10), filter(unique(A2:A7), not(isblank(unique(A2:A7)))))&";"
Use a static dictionary object in a user defined functiuon (aka UDF) and overwrite any duplicates. 在用户定义的函数(也称为UDF)中使用静态字典对象,并覆盖所有重复项。
Option Explicit
Function udfUniqueList(str As String, _
delim As String, _
Optional cs As Boolean = False)
Dim a As Long, arr As Variant
Static dict As Object
If dict Is Nothing Then
Set dict = CreateObject("Scripting.Dictionary")
End If
dict.RemoveAll
dict.CompareMode = IIf(cs, vbBinaryCompare, vbTextCompare)
arr = Split(str, Chr(10))
For a = LBound(arr) To UBound(arr)
dict.Item(arr(a)) = a
Next a
udfUniqueList = Join(dict.keys, delim) & delim
End Function
A static object is best for functions that will be copied down a long column as the object does not have to be recreated for repetitious use. 静态对象最适合将其复制到长列的函数,因为不必为重复使用而重新创建该对象。
Remember to turn 'wrap text' on in the destination cell. 请记住在目标单元格中打开“自动换行”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.