excel行到逗号分隔

excel行到逗号分隔,excel,xls,xlsx,vba,Excel,Xls,Xlsx,Vba,我有一个xls文件,其值如下: ID Value 1 value1 1 value2 1 value3 2 value1 2 value2 3 value1 3 value2 等 我想将此转换为(创建新工作表) 基本上,我希望所有具有相同ID的行连接所有以逗号分隔的值 谢谢,请尝试以下代码: Sub sample() Dim i As Integer, j As Integer Dim temp As String Range("A:A").AdvancedFilter

我有一个xls文件,其值如下:

ID Value
1 value1
1 value2
1 value3
2 value1
2 value2
3 value1
3 value2

我想将此转换为(创建新工作表)

基本上,我希望所有具有相同ID的行连接所有以逗号分隔的值

谢谢,

请尝试以下代码:

Sub sample()
    Dim i As Integer, j As Integer
    Dim temp As String
    Range("A:A").AdvancedFilter Action:=xlFilterCopy, copytorange:=Range("C1"), unique:=True

    Dim lastRow As Long
    lastRow = Range("C65000").End(xlUp).Row

    j = 1
     For i = 2 To lastRow
        Do Until Cells(j, 2) = ""
            If Trim(Cells(j, 1)) = Trim(Cells(i, 3)) Then
                temp = temp & "," & Cells(j, 2)
            End If
            j = j + 1
        Loop
        Cells(i, 4) = temp
        temp = ""
        j = 1
    Next
End Sub


非常感谢,这就行了。我刚刚添加了temp=Right(temp,Len(temp)-1)来删除第17行的第一个逗号。
Sub sample()
    Dim i As Integer, j As Integer
    Dim temp As String
    Range("A:A").AdvancedFilter Action:=xlFilterCopy, copytorange:=Range("C1"), unique:=True

    Dim lastRow As Long
    lastRow = Range("C65000").End(xlUp).Row

    j = 1
     For i = 2 To lastRow
        Do Until Cells(j, 2) = ""
            If Trim(Cells(j, 1)) = Trim(Cells(i, 3)) Then
                temp = temp & "," & Cells(j, 2)
            End If
            j = j + 1
        Loop
        Cells(i, 4) = temp
        temp = ""
        j = 1
    Next
End Sub