连接从行到数组Excel的值

时间:2013-11-12 08:30:45

标签: excel

我在excel中有一些数据需要从重复行转换为1行数组,我正在尝试查找>转到特殊,按值替换空白,但行不等于。

Excel数据示例:

Code    Image
112001  image1.jpg
112001  image2.jpg
112001  image3.jpg

112004  image1.jpg
112004  image2jpg

554551  image1.jpg
554551  image2.jpg
554551  image3.jpg
554551  image4.jpg

rusult需要

Code    Image
112001  image1.jpg  image1.jpg,image2.jpg,image3.jpg
112001  image2.jpg
112001  image3.jpg

112004  image1.jpg  image1.jpg,image2.jpg
112004  image2jpg

554551  image1.jpg  image1.jpg,image2.jpg,image3.jpg,image4.jpg
554551  image2.jpg
554551  image3.jpg
554551  image4.jpg

问题是我有7.000行并且需要以编程方式进行。

非常欢迎一些关于阵列的建议。

1 个答案:

答案 0 :(得分:1)

我玩了一小部分数据,并提出了下面的代码。

Sub CompareAndOutput()

    Dim lastRow As Long
    Dim ws As Worksheet

    Dim row As Long, col As Long, recordCount As Long

    Dim output As String
    Dim chkStr As String, prevStr As String

    On Error GoTo err

    'set sheet we want to use
    Set ws = Sheet1

    'set check column to A
    col = 1

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual



    lastRow = ws.Cells(ws.Rows.Count, col).End(xlUp).row

        'loop through rows
        For row = 1 To lastRow + 1

            'set column A string to compare
            chkStr = ws.Cells(row, col).Value

            'compare string to previous value
            If chkStr <> prevStr Then

            'output string at bottom of group
            ws.Cells(row - recordCount, 3).Value = output

            'clear values
            output = ""
            recordCount = 0
            End If

            'build output string
            output = output & " " & ws.Cells(row, 2).Value

            recordCount = recordCount + 1

            'set previous string for comparison next loop
            prevStr = chkStr

        Next row


    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

Exit Sub
err:
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

    MsgBox err.Description, vbCritical, "An error occured"

End Sub

它遍历组顶部的行和输出...列A组必须在一起才能使它工作..