Batch string concatenation in Excel

后端 未结 12 2111
猫巷女王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:09

    Press Alt-F11, insert new module, paste code bellow.

    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
    

    Usage:

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

    or

    =concatRange(A8:D11)
    

提交回复
热议问题