Batch string concatenation in Excel

后端 未结 12 2109
猫巷女王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 11:10

    This VBA function will concatenate the contents of cells, with an optional delimiter, if needed. Copy it into a standard module:

      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
    

提交回复
热议问题