[英]How to merge rows in a column into one cell in excel?
Eg例如
A1:I
A2:am
A3:a
A4:boy
I want to merge them all to a single cell "Iamaboy"我想将它们全部合并到一个单元格“Iamaboy”中
This example shows 4 cells merge into 1 cell however I have many cells (more than 100), I can't type them one by one using A1 & A2 & A3 & A4
what can I do?此示例显示 4 个单元格合并为 1 个单元格,但是我有很多单元格(超过 100 个),我无法使用
A1 & A2 & A3 & A4
它们一个一个地键入,我该怎么办?
If you prefer to do this without VBA, you can try the following: 如果您希望在不使用VBA的情况下执行此操作,则可以尝试以下操作:
Cell B999 will now contain the concatenated text string you are looking for. 现在,单元格B999将包含您要查找的串联文本字符串。
I present to you my ConcatenateRange VBA function (thanks Jean for the naming advice!) . 我向您展示了我的ConcatenateRange VBA函数(感谢Jean的命名建议!)。 It will take a range of cells (any dimension, any direction, etc.) and merge them together into a single string.
它将需要一个单元格范围(任何尺寸,任何方向等),并将它们合并到一个字符串中。 As an optional third parameter, you can add a seperator (like a space, or commas sererated).
作为可选的第三个参数,您可以添加分隔符(例如空格或带逗号的逗号)。
In this case, you'd write this to use it: 在这种情况下,您可以编写以下代码来使用它:
=ConcatenateRange(A1:A4)
Function ConcatenateRange(ByVal cell_range As range, _
Optional ByVal separator As String) As String
Dim newString As String
Dim cell As Variant
For Each cell in cell_range
If Len(cell) <> 0 Then
newString = newString & (separator & cell)
End if
Next
If Len(newString) <> 0 Then
newString = Right$(newString, (Len(newString) - Len(separator)))
End If
ConcatenateRange = newString
End Function
Inside CONCATENATE
you can use TRANSPOSE
if you expand it ( F9 ) then remove the surrounding {}
brackets like this recommends 在
CONCATENATE
内,如果您将其展开( F9 ),则可以使用TRANSPOSE
,然后按照以下建议删除周围的{}
括号
=CONCATENATE(TRANSPOSE(B2:B19))
Becomes 成为
=CONCATENATE("Oh ","combining ", "a " ...)
You may need to add your own separator on the end, say create a column C and transpose that column. 您可能需要在最后添加自己的分隔符,例如创建C列并转置该列。
=B1&" "
=B2&" "
=B3&" "
In simple cases you can use next method which doesn`t require you to create a function or to copy code to several cells: 在简单的情况下,您可以使用不需要创建函数或将代码复制到多个单元格的下一个方法:
In any cell write next code 在任何单元格中编写下一个代码
=Transpose(A1:A9)
Where A1:A9 are cells you would like to merge. 其中A1:A9是您要合并的单元格。
F9
F9
After that, the cell will contain the string: 之后,单元格将包含字符串:
={A1,A2,A3,A4,A5,A6,A7,A8,A9}
Source: http://www.get-digital-help.com/2011/02/09/concatenate-a-cell-range-without-vba-in-excel/ 来源: http : //www.get-digital-help.com/2011/02/09/concatenate-a-cell-range-without-vba-in-excel/
Update: One part can be ambiguous. 更新:一部分可能是模棱两可的。 Without leaving the cell means having your cell in editor mode.
不离开单元格意味着将单元格置于编辑器模式。 Alternatevly you can press F9 while are in cell editor panel (normaly it can be found above the spreadsheet)
或者,您可以在单元格编辑器面板中按F9键(通常可以在电子表格上方找到它)
Use VBA's already existing Join
function. 使用VBA已有的
Join
函数。 VBA functions aren't exposed in Excel, so I wrap Join
in a user-defined function that exposes its functionality. VBA函数未在Excel中公开,因此我将
Join
包装在一个用户定义的函数中,该函数公开了其功能。 The simplest form is: 最简单的形式是:
Function JoinXL(arr As Variant, Optional delimiter As String = " ")
'arr must be a one-dimensional array.
JoinXL = Join(arr, delimiter)
End Function
Example usage: 用法示例:
=JoinXL(TRANSPOSE(A1:A4)," ")
entered as an array formula (using Ctrl - Shift - Enter ). 输入为数组公式(使用Ctrl - Shift - Enter )。
Now, JoinXL
accepts only one-dimensional arrays as input. 现在,
JoinXL
仅接受一维数组作为输入。 In Excel, ranges return two-dimensional arrays. 在Excel中,范围返回二维数组。 In the above example,
TRANSPOSE
converts the 4×1 two-dimensional array into a 4-element one-dimensional array (this is the documented behaviour of TRANSPOSE
when it is fed with a single-column two-dimensional array). 在上面的示例中,
TRANSPOSE
将4×1二维数组转换为4元素一维数组(这是TRANSPOSE
在馈入单列二维数组时记录的行为)。
For a horizontal range, you would have to do a double TRANSPOSE
: 对于水平范围,您必须执行两次
TRANSPOSE
:
=JoinXL(TRANSPOSE(TRANSPOSE(A1:D1)))
The inner TRANSPOSE
converts the 1×4 two-dimensional array into a 4×1 two-dimensional array, which the outer TRANSPOSE
then converts into the expected 4-element one-dimensional array. 内部
TRANSPOSE
将1×4二维数组转换为4×1二维数组,然后外部TRANSPOSE
将其转换为预期的4元素一维数组。
This usage of TRANSPOSE
is a well-known way of converting 2D arrays into 1D arrays in Excel, but it looks terrible. TRANSPOSE
这种用法是在Excel中将2D数组转换为1D数组的一种众所周知的方法,但是看起来很糟糕。 A more elegant solution would be to hide this away in the JoinXL
VBA function. 更好的解决方案是将其隐藏在
JoinXL
VBA函数中。
For those who have Excel 2016 (and I suppose next versions), there is now directly the CONCAT function, which will replace the CONCATENATE function. 对于拥有Excel 2016 (我想是下一版本)的用户,现在直接有CONCAT函数,它将替换CONCATENATE函数。
So the correct way to do it in Excel 2016 is : 因此在Excel 2016中执行此操作的正确方法是:
=CONCAT(A1:A4)
which will produce : 会产生:
Iamaboy
我是一个男孩
For users of olders versions of Excel, the other answers are relevant. 对于较早版本的Excel的用户,其他答案也很重要。
For Excel 2011 on Mac it's different. 对于Mac上的Excel 2011,情况有所不同。 I did it as a three step process.
我将其分为三个步骤进行。
=B1
. =B1
。 For the next row to row N, the formula is =Concatenate(",",A2)
. =Concatenate(",",A2)
。 You end up with: QA ,Sekuli ,Testing ,Applitools ,Visual Testing ,Test Automation ,Selenium
=B1
. =B1
。 For all other rows to N, the formula is =Concatenate(C1,B2)
. =Concatenate(C1,B2)
。 And you get: QA,Sekuli QA,Sekuli,Testing QA,Sekuli,Testing,Applitools QA,Sekuli,Testing,Applitools,Visual Testing QA,Sekuli,Testing,Applitools,Visual Testing,Test Automation QA,Sekuli,Testing,Applitools,Visual Testing,Test Automation,Selenium
The last cell of the list will be what you want. 列表的最后一个单元格将是您想要的。 This is compatible with Excel on Windows or Mac.
这与Windows或Mac上的Excel兼容。
I use the CONCATENATE
method to take the values of a column and wrap quotes around them with columns in between in order to quickly populate the WHERE IN ()
clause of a SQL statement. 为了快速填充SQL语句的
WHERE IN ()
子句,我使用CONCATENATE
方法获取列的值,并用引号将引号引起来。
I always just type =CONCATENATE("'",B2,"'",",")
and then select that and drag it down, which creates =CONCATENATE("'",B3,"'",",")
, =CONCATENATE("'",B4,"'",",")
, etc. then highlight that whole column, copy paste to a plain text editor and paste back if needed, thus stripping the row separation. 我总是只键入
=CONCATENATE("'",B2,"'",",")
,然后选择并将其向下拖动,即可创建=CONCATENATE("'",B3,"'",",")
, =CONCATENATE("'",B4,"'",",")
等,然后突出显示整个列,将粘贴复制到纯文本编辑器,并在需要时粘贴回去,从而去除行分隔。 It works, but again, just as a one time deal, this is not a good solution for someone who needs this all the time. 它可以工作,但是再次,就像一次交易一样,对于一直需要它的人来说,这不是一个好的解决方案。
I know this is really a really old question, but I was trying to do the same thing and I stumbled upon a new formula in excel called "TEXTJOIN".我知道这真的是一个非常古老的问题,但我试图做同样的事情,我偶然发现了 Excel 中一个名为“TEXTJOIN”的新公式。
For the question, the following formula solves the problem对于问题,下面的公式解决了问题
=TEXTJOIN("",TRUE,(a1:a4))
The signature of "TEXTJOIN" is explained as TEXTJOIN(delimiter,ignore_empty,text1,[text2],[text3],...) “TEXTJOIN”的签名解释为 TEXTJOIN(delimiter,ignore_empty,text1,[text2],[text3],...)
I needed a general purpose Concatenate With Separator ( since I don't have TEXTJOIN ) so I wrote this:我需要一个通用的连接分隔符(因为我没有 TEXTJOIN )所以我写了这个:
Public Function ConcatWS(separator As String, ParamArray cell_range()) As String
'---concatenate with seperator
For n = LBound(cell_range) To UBound(cell_range)
For Each cell In cell_range(n)
If Len(cell) <> 0 Then
ConcatWS = ConcatWS & IIf(ConcatWS <> "", separator, "") & cell
End If
Next
Next n
End Function
Which allows us to go crazy with flexibility in including cell ranges:这让我们可以灵活地包含单元格范围:
=ConcatWS(" ", Fields, E1:G2, L6:M9, O6)
NOTE: "Fields" is a Named Range and the separator may be blank注意:“字段”是命名范围,分隔符可能为空
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.