简体   繁体   English

用于连接单个单元格中每一行的值的 Excel 公式

[英]Excel formula for concatenating the values for each row in a single cell

I am trying to get following output in excel using excel formula only我正在尝试仅使用 excel 公式在 excel 中获得以下输出

My inputs are numbers between 1 and 10 in each row.我的输入是每行 1 到 10 之间的数字。 I need the output to be a string containing the inputs separated by or in a single cell我需要输出是一个字符串,其中包含由单个单元格分隔or在单个单元格中的输入

A   B
1   1 or 2 or 3 or 4
2   
3   
4   

Thanks谢谢

Given you are using Excel 2007 , I have put a few options below.鉴于您使用的是 Excel 2007 ,我在下面提供了一些选项。

Unfortunately I don't think there is any nice way to do this with Excel's native functions - hence why they created TEXTJOIN in Excel 2016不幸的是,我认为使用 Excel 的本机函数没有什么好的方法可以做到这一点 - 因此他们在 Excel 2016 中创建了TEXTJOIN

Option 1选项1
The most obvious solution is to explicitly write out the formula.最明显的解决方案是明确写出公式。 If you only need to do this one time and there aren't many values (eg 4 values in the example) then this is just fine如果您只需要执行一次并且没有很多值(例如示例中的 4 个值),那么这很好

=A1&" or "&A2&" or "&A3&" or "&A4

Option 2选项 2
Alternatively, if you wish to use this a few different times with potentially many values.或者,如果您希望多次使用它,可能会有很多值。 I'd suggest you use a UDF (User Defined Function).我建议您使用UDF (用户定义函数)。 That is, write a function that does exactly what you require such that you use the function name on the worksheet like this:也就是说,编写一个完全符合您要求的函数,以便您在工作表上使用函数名称,如下所示:

=JoinWithOr(A1:A4)

giving the output 1 or 2 or 3 or 4 -- for example.例如,给出输出1 or 2 or 3 or 4

The code for that function is below.该函数的代码如下。 You need to open the VBA editor, add a module and paste the code in there to be able to use the function.您需要打开 VBA 编辑器,添加一个module并将代码粘贴到其中才能使用该功能。

Function JoinWithOr(rng As Range) As String
    Dim r As Range
    Dim result As String, orString As String

    orString = " or "

    For Each r In rng
        result = result & r.Value & orString
    Next r

    JoinWithOr = Left(result, Len(result) - Len(orString)) 
End Function

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

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