Batch string concatenation in Excel

后端 未结 12 2091
猫巷女王i
猫巷女王i 2020-12-16 10:31

I have a couple hundred of cells in Excel I would like to concatenate into a single string. Is there a simpler method of doing this than going through them one by one manual

相关标签:
12条回答
  • 2020-12-16 10:57

    *In a new tab, type A1 in cell A1,

    *Type A2 in Cell A2

    *Use fill series to complete the values in column A

    *Type A1 in cell B1

    • Use this forumal in cell B2

      =B1&","&A2

    • Copy the formula down.

    Copy and paste values to harvest the string of values you created.

    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
    
    0 讨论(0)
  • 2020-12-16 10:57

    My preferred method is to cut-and-paste the values into an editor that allows regular expressions, then I simply remove the tabs (or spaces) with a find and replace on my current selection.

    You can also use this to insert commas, whitespace, or whatever you want.

    It's a ton faster than typing =concatenate(A1,",","A2",",",......)

    0 讨论(0)
  • 2020-12-16 10:59

    See this blog post here: http://www.dullsharpness.com/2011/11/14/excel-vba-range-to-csv-range2csv-function/

    You can use it like so, e.g. with a pipe delimiter:

    =Range2Csv(A1:A255,"|")
    

    Access your VBA editor using Alt+F11 and drop it into a module.

    Code excerpt is here:

    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
    
    0 讨论(0)
  • 2020-12-16 10:59

    Shamelessly copied from this site:

    1. Select the cell where you need the result.
    2. Go to formula bar and enter ... "=A1:A5"
    3. Select the entire formula and press F9 (this converts the formula into values).
    4. Remove the curly brackets from both ends.
    5. Add =CONCATENATE( to the beginning of the text and end it with a round bracket).
    6. Press Enter.

    What is particularly revelatory here is that when editing a formula, pressing F9 replaces the formula with the result of that formula. Where that's a range, it replaces it with a list of the contents of that range.

    0 讨论(0)
  • 2020-12-16 11:00

    It isn't purely Excel, but there is an easy way to do this with Word.

    1. Select the cells you want to concatenate and copy/paste them into Word. This creates a table.
    2. Select the entire table.
    3. Convert the table to text. Use paragraph marks (or something else that does not appear in your text) as separators.
    4. Select all of the text.
    5. Use Replace to remove the paragraph marks. (In the "Find what" box, enter ^p. Leave the "Replace with" box empty.)
    0 讨论(0)
  • 2020-12-16 11:06

    If you have Excel 2016, you can use an array formula:

    Enter

    =concat(a1:a255)

    into the cell, then press

    [ctrl]+[shift]+[enter]

    0 讨论(0)
提交回复
热议问题