简体   繁体   English

查找多个值并返回单个字符串

[英]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.

相关问题 "JavaScript:在字符串中搜索多个值并返回匹配项" - JavaScript: search for multiple values in a string and return the match 如何在单个数组中返回多个 function 值 - How can I return multiple function values in a single array 如何以单个字符串的形式从多行中检索值 - How to retrieve values from multiple rows as a single string 如何使用数组为Java中的单个字符串分配多个值? - How to assign multiple values to a single string in Java with an array? 将具有单个键和字符串值的数组拆分为具有多个键和值的数组 - splitting an array with single key and string value into an array with multiple keys and values 在Javascript中从一串数组中查找单个或多个值 - Finding a single, or multiple values from a string of arrays in Javascript Rails中的数据结构可对多个值进行排序和查找 - Data Structure in Rails to sort and lookup on multiple values Postgres数组在where子句中查找多个值 - Postgres array lookup multiple values in where clause 查找多列并在excel中求和它们的值 - Lookup multiple columns and sum their values in excel Excel Match Multiple Criteria Lookup Array:从列表中查找包含所有值的列,并返回数组中的列 position - Excel Match Multiple Criteria Lookup Array: Find columns that contains all values from a list and return the columns position in the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM