Excel中的批处理字符串连接

时间:2013-02-28 20:14:22

标签: excel excel-formula concatenation string-concatenation

我在Excel中有几百个单元格我想连接成一个单独的字符串。是否有一种更简单的方法可以手动逐一进行,以便将它们输入CONCATENATE(A1, A2, ....)

CONCATENATE(A1:A255)不起作用。

12 个答案:

答案 0 :(得分:34)

*在新标签中,在单元格A1中键入A1,

*在A2单元格中输入A2

*使用填充系列来完成A列中的值

*在单元格B1中键入A1

  • 在单元格B2中使用此正式

    = B1&安培; “” &安培; A2

  • 将公式复制下来。

复制并粘贴值以获取您创建的值字符串。

A1  A1
A2  A1,A2
A3  A1,A2,A3
A4  A1,A2,A3,A4
A5  A1,A2,A3,A4,A5
A6  A1,A2,A3,A4,A5,A6
A7  A1,A2,A3,A4,A5,A6,A7
A8  A1,A2,A3,A4,A5,A6,A7,A8
A9  A1,A2,A3,A4,A5,A6,A7,A8,A9
A10 A1,A2,A3,A4,A5,A6,A7,A8,A9,A10

答案 1 :(得分:13)

Alt-F11,插入新模块,粘贴以下代码。

Public Function concatRange(data As Range, Optional sep As String = "") As String
    Dim ret As String
    Dim sep2 As String
    ret = ""
    sep2 = ""

    For Each cell In data
        ret = ret & sep2 & cell.Value
        sep2 = sep
    Next cell

    concatRange = ret
End Function

用法:

=concatRange(A8:D11;", ")    'OS with ; list separator
=concatRange(A8:D11,", ")    'OS with , list separator or in a macro code

=concatRange(A8:D11)

答案 2 :(得分:8)

请在此处查看此博文:http://www.dullsharpness.com/2011/11/14/excel-vba-range-to-csv-range2csv-function/

您可以像这样使用它,例如使用管道分隔符:

=Range2Csv(A1:A255,"|")

使用Alt+F11访问您的VBA编辑器并将其放入模块中。

代码摘录在这里:

Option Explicit
'**********************************************
'* PURPOSE: Concatenates range contents into a
'*          delimited text string
'*
'* FUNCTION SIGNATURE: Range2Csv(Range, String)
'*
'* PARAMETERS:
'*    Range  - the range of cells whose contents
'*             will be included in the CSV result
'*    String - delimiter used to separate values
'*             (Optional, defaults to a comma)
'*
'* AUTHOR: www.dullsharpness.com
'*
'* NOTES: [add'l notes removed for brevity]
'*
'**********************************************
Public Function Range2Csv(inputRange As Range, Optional delimiter As String)
  Dim concattedList As String 'holder for the concatted CSVs
  Dim rangeCell As Range      'holder cell used in For-Each loop
  Dim rangeText As String     'holder for rangeCell's text

  'default to a comma delimiter if none is provided
  If delimiter = "" Then delimiter = ","

  concattedList = ""          'start with an empty string

  'Loop through each cell in the range to append valid contents
  For Each rangeCell In inputRange.Cells

    rangeText = rangeCell.Value 'capture the working value

    'Only operate on non-blank cells (i.e. Length > 0)
    If Len(rangeText) > 0 Then
      'Strip any delimiters contained w/in the value itself
      rangeText = WorksheetFunction.Substitute(rangeText, delimiter, "")

      If (Len(concattedList) > 0) Then
        'prepend a delimiter to the new value if we
        'already have some list items
        concattedList = concattedList + delimiter + rangeText
      Else
        'else if the list is blank so far,
        'just set the first value
        concattedList = rangeText
      End If
    End If

  Next rangeCell

  'Set the return value
  Range2Csv = concattedList

End Function

答案 3 :(得分:7)

  

连接(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23 ,a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,a40,a41,a42,a43,a44,a45,a46,a47,a48 ,a49,a50,a51,a52,a53,a54,a55,a56,a57,a58,a59,a60,a61,a62,a63,a64,a65,a66,a67,a68,a69,a70,a71,a72,a73 ,a74,a75,a76,a77,a78,a79,a80,a81,a82,a83,a84,a85,a86,a87,a88,a89,a90,a91,a92,a93,a94,a95,a96,a97,a98 ,a99,a100,a101,a102,a103,a104,a105,a106,a107,a108,a109,a110,a111,a112,a113,a114,a115,a116,a117,a118,a119,a120,a121,a122,a123 ,a124,a125,a126,a127,a128,a129,a130,a131,a132,a133,a134,a135,a136,a137,a138,a139,a140,a141,a142,a143,a144,a145,a146,a147,a148 ,a149,a150,a151,a152,a153,a154,a155,a156,a157,a158,a159,a160,a161,a162,a163,a164,a165,a166,a167,a168,a169,a170,a171,a172,a173 ,a174,a175,a176,a177,a178,a179,a180,a181,a182,a 183,a184,a185,a186,a187,a188,a189,a190,a191,a192,a193,a194,a195,a196,a197,a198,a199,a200,a201,a202,a203,a204,a205,a206,a207, a208,a209,a210,a211,a212,a213,a214,a215,a216,a217,a218,a219,a220,a221,a222,a223,a224,a225,a226,a227,a228,a229,a230,a231,a232, a233,a234,a235,a236,a237,a238,a239,a240,a241,a242,a243,a244,a245,a246,a247,a248,a249,a250,a251,a252,a253,a254,a255)

PowerShell it!

"concatenate(a$((1..255) -join ', a'))" | clip

打开文本文件复制并粘贴

"To quickly select cells you can press CTRL and click on cells you want to be included in the concatenate function. Example, Select a cell Type =concatenate( in formula bar Press and hold CTRL button and click cells to be included. Release CTRL button Type ) in formula bar and press Enter"

答案 4 :(得分:3)

如果需要,此VBA函数将连接单元格的内容,并带有可选的分隔符。将其复制到标准模块中:

  Option Explicit

  Function Concat(CellRange As Range, Optional Delimiter As String) As String 
 ' this function will concatenate a range of cells and return the result as a single string
 ' useful when you have a large range of cells that you need to concatenate
 ' source: http://chandoo.org/wp/2008/05/28/how-to-add-a-range-of-cells-in-excel-concat/

  Dim retVal As String, dlm As String, cell As Range
  retVal = ""
  If Delimiter = Null Then
      dlm = ""
  Else
    dlm = Delimiter
  End If
  For Each cell In CellRange
      If CStr(cell.Value) <> "" And CStr(cell.Value) <> " " Then
          retVal = retVal & CStr(cell.Value) & dlm
      End If
  Next
  If dlm <> "" Then
      retVal = Left(retVal, Len(retVal) - Len(dlm))
  End If
  Concat = retVal
End Function

答案 5 :(得分:2)

如果您正在寻找纯Excel方法(即没有VBA),那么James Jenkins提出的方法是最好的。 如果您对使用VBA感到满意,请打开VBA编辑器,添加新模块,然后添加以下代码:

Option Explicit

Public Function JoinText(cells As Variant,Optional delim_str As String) As String
    If cells.Columns.count < cells.Rows.count Then
       JoinText = Join(WorksheetFunction.Transpose(cells), delim_str)
    Else
       JoinText = Join(WorksheetFunction.Transpose(WorksheetFunction.Transpose(cells)), delim_str)
    End If
End Function

要轻松打开VBA编辑器,请按Alt-F11。 要插入模块,请右键单击“项目”窗口中列出的工作簿。

从excel调用该函数如下:

=JoinText(A1:C1)

如果要添加分隔符(例如逗号):

=JoinText(A1:C1,",")

使用转置功能的目的是将2d数组'cells'转换为1d数组。对此的共鸣是VBA函数Join仅接受1d数组。 使用其中两个的原因是,如果JoinText正在查看一行单元格(仍然只是一个2d数组),那么第一次调用transpose,将这个2d行数组转换为2d列数组,第二次调用将其转换为1d阵列。

答案 6 :(得分:2)

我首选的方法是将值剪切并粘贴到允许正则表达式的编辑器中,然后我只需删除选项卡(或空格),并在我当前的选择中替换。

您也可以使用它来插入逗号,空格或任何您想要的内容。

比输入=concatenate(A1,",","A2",",",......)

更快

答案 7 :(得分:1)

这不是纯粹的Excel,但有一种简单的方法可以使用Word。

  1. 选择要连接的单元格,然后将其复制/粘贴到Word中。这会创建一个表格。
  2. 选择整个表格。
  3. 将表格转换为文字。使用段落标记(或文本中未出现的其他内容)作为分隔符。
  4. 选择所有文字。
  5. 使用“替换”删除段落标记。 (在“查找内容”框中,输入^ p。将“替换为”框保留为空。)

答案 8 :(得分:1)

如果您使用的是Excel 2016,则可以使用数组公式:

输入

  

= CONCAT(A1:A255)

进入单元格,然后按

  

[CTRL] + [SHIFT] + [输入]

答案 9 :(得分:0)

您希望连接的值从表单的第2行第3列开始

Sub GOWN()
roww = 2
Do While cells(roww, 2) <> ""
    aa = cells(roww, 3)
    dd = dd & aa & ","
    roww = roww + 1
Loop
cells(roww + 1, 3) = dd
End Sub

答案 10 :(得分:0)

只需在一个连接中添加分隔符:

=concatenate(A1, ",")

然后复制所有连接,将它们粘贴为值。然后复制这些值,将它们粘贴到转置中。然后复制Transposed值并将它们粘贴到word编辑器中。找到deliminator和值前面的空格,然后替换JUST deiminator。这应该为您提供带有分隔符的所有值的串联字符串。这比其他选项容易得多。

答案 11 :(得分:0)

this site无耻地复制:

  
      
  1. 选择您需要结果的单元格。
  2.   
  3. 转到公式栏并输入...“= A1:A5”
  4.   
  5. 选择整个公式并按F9(这会将公式转换为值)。
  6.   
  7. 从两端移除花括号。
  8.   
  9. 添加= CONCATENATE(到文本的开头,用圆括号结束)。
  10.   
  11. 按Enter键。
  12.   

这里特别具有启发性的是,在编辑公式时,按F9会将公式替换为该公式的结果。如果这是一个范围,它将用该范围的内容列表替换它。