Excel连接列

时间:2016-10-21 10:14:54

标签: excel

我正在使用Excel,我需要将产品ID与其旁边的逗号连接起来。示例列A包含1-1760个产品ID,我想用逗号(,)连接所有产品ID。我想在B2列(= 1,2,3,4,5,6,7,8)的一行中添加这些内容。

enter image description here

2 个答案:

答案 0 :(得分:2)

这就是你要找的东西:

Option Explicit

Sub TestJoin()

Dim MyRange As Range
Dim MyArray() As Variant

Set MyRange = Range("A1:A5")
MyArray = Application.Transpose(MyRange)
Range("B1") = Join(MyArray, ",")

End Sub

答案 1 :(得分:0)

您需要以下内容:

Public Sub test()
    Dim i As Long

    ActiveSheet.Range("A2").Select
    i = 0
    Do
        ActiveSheet.Range("B2").Value = ActiveSheet.Range("B2").Value _
        + CStr(ActiveCell.Offset(i, 0)) + ","
        i = i + 1

    'change 21 to whatever number you want to reach (or use a variable/constant)
    Loop While i <= 21 - 2 
End Sub

然而,根据你从上面得到的结果你必须删除上面的函数将自动放置的最后添加的昏迷。您可以使用vba的left()功能与len()-1

结合使用