Excel – 列到一个单元格

Joh*_*ohn 10 microsoft-excel

您能否将一列数据移动到单个单元格中,并用逗号分隔该列中的值?一种反向文本到列。

例如,

1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)

1,2,3,4,5单个单元格中。

bre*_*tdj 7

使用用户定义的函数比逐个单元地硬编码要灵活得多

  • 同时按转到 VBE
  • 插入模块
  • 复制并粘贴下面的代码
  • 同时按返回 Excel

使用您的新公式,就像 D6 单元格快照中的公式一样
=ConCat(A1:A5)

您可以使用更复杂的公式,例如 D7
=ConCat(A1:A5,A7:A24)
或 D8 中的公式,即 2D
=concat(A1:B5,A7:A24)

样本

Function ConCat(ParamArray rng1()) As String
    Dim X
    Dim strOut As String
    Dim strDelim As String
    Dim rng As Range
    Dim lngRow As Long
    Dim lngCol As Long
    Dim lngCnt As Long
    strDelim = ", "
    For lngCnt = LBound(rng1) To UBound(rng1)
        If TypeOf rng1(lngCnt) Is Range Then
            If rng1(lngCnt).Cells.Count > 1 Then
                X = rng1(lngCnt).Value2
                For lngRow = 1 To UBound(X, 1)
                    For lngCol = 1 To UBound(X, 2)
                        strOut = strOut & (strDelim & X(lngRow, lngCol))
                    Next
                Next
            Else
                strOut = strOut & (strDelim & rng2.Value)
            End If
        End If
    Next
    ConCat = Right$(strOut, Len(strOut) - Len(strDelim))
End Function
Run Code Online (Sandbox Code Playgroud)


小智 5

  • 首先,做

    =concatenate(A1,",")
    
    Run Code Online (Sandbox Code Playgroud)

    在您有值的旁边的下一列中。

  • 其次,复制整个列并转到另一个工作表执行选择性粘贴-> 转置。

  • 第三步复制刚刚得到的值,打开一个word文档,然后选择粘贴选项->选择“A”,
  • 最后,将word文档中的所有内容复制回excel表中的一个单元格,您将获得一个单元格中的所有值


shu*_*ler 2

您可以使用连接函数并在单元格和字符串之间交替","

=CONCATENATE(A1,",",A2,",",A3,",",A4,",",A5)
Run Code Online (Sandbox Code Playgroud)