简体   繁体   English

如何将一列中的行合并到excel中的一个单元格中?

[英]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的情况下执行此操作,则可以尝试以下操作:

  1. Have your data in cells A1:A999 (or such) 将数据存储在单元格A1:A999中(或诸如此类)
  2. Set cell B1 to "=A1" 将单元格B1设置为“ = A1”
  3. Set cell B2 to "=B1&A2" 将单元格B2设置为“ = B1&A2”
  4. Copy cell B2 all the way down to B999 (eg by copying B2, selecting cells B3:B99 and pasting) 将单元格B2一直复制到B999(例如,通过复制B2,选择单元格B3:B99并粘贴)

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 " ...)

CONCATENATE(TRANSPOSE(B2:B19))

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: 在简单的情况下,您可以使用不需要创建函数或将代码复制到多个单元格的下一个方法:

  1. In any cell write next code 在任何单元格中编写下一个代码

     =Transpose(A1:A9) 

Where A1:A9 are cells you would like to merge. 其中A1:A9是您要合并的单元格。

  1. Without leaving the cell press 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. 我将其分为三个步骤进行。

  1. Create a column of values in column A. 在A列中创建一列值。
  2. In column B, to the right of the first cell, create a rule that uses the concatenate function on the column value and ",". 在第一个单元格右侧的B列中,创建一个在列值和“,”上使用串联函数的规则。 For example, assuming A1 is the first row, the formula for B1 is =B1 . 例如,假设A1是第一行,则B1的公式为=B1 For the next row to row N, the formula is =Concatenate(",",A2) . 对于N行的下一行,公式为=Concatenate(",",A2) You end up with: 您最终得到:
QA
,Sekuli
,Testing
,Applitools
,Visual Testing
,Test Automation
,Selenium
  1. In column C create a formula that concatenates all previous values. 在C列中,创建一个将所有先前值连接在一起的公式。 Because it is additive you will get all at the end. 因为它是可加性的,您将在最后得到全部。 The formula for cell C1 is =B1 . 单元格C1的公式为=B1 For all other rows to N, the formula is =Concatenate(C1,B2) . 对于N的所有其他行,公式为=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.

相关问题 如何根据Excel中一列中的值合并行? - How to merge rows based on value in one column in Excel? 有条件地将某些行中的单元格合并到 Excel 中的一个单元格中 - Merge cells conditionally from certain rows into one cell in Excel 如何在Excel中将多列转换为一列,但保留每一列的第一个单元格并在新行中重复呢? - How to convert many columns to one column in excel but keep the 1st cell of each column and repeat it in new rows? Excel - 合并具有共同值的行并连接一列中的差异 - Excel - Merge rows with common values and concatenate the differences in one column 如何在每个单元格中具有一排以​​上的列的大熊猫中读取Excel文件 - How to read excel file in pandas with a column with more than one rows within each cell 将多个列行合并到一个单元格中 - Merge multiple column rows into a single cell 如何将Excel的单个单元格中的数据格式化为正确的列和行? - How to format data which is in single cell of excel in to proper column and rows? 如果第一列中的单元格不是粗体,如何删除excel中的行? - How to delete rows in excel if the cell in the first column isn't bold? 如果单元格/列不包含文本,如何删除Excel中的行? - How to I delete rows in Excel if a cell/column does not contain text? 如何从Excel中的某个单元格开始获取A列中的行数 - How to get the number of rows in column A starting from a certain cell in excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM