excel row to comma separated

旧城冷巷雨未停 提交于 2019-12-13 22:02:47

问题


I have an xls file with values of the following:

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

etc

I would like to transform this to (create a new sheet)

ID Values
1 value1,value2,value3
2 value1,value2
3 value1,value2,value3

Basically, I would like all rows that have the same ID to join all values comma separated.

Thanks,


回答1:


Try below code :

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



来源:https://stackoverflow.com/questions/15598423/excel-row-to-comma-separated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!