0

我在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

的结果需要是

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 行,需要以编程方式进行。

一些做数组的建议是非常受欢迎的。

4

1 回答 1

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列组必须在一起才能工作......

于 2013-11-12T09:25:29.540 回答