VBA宏用于复制和粘贴来自重复单元格的单元格

时间:2015-12-08 14:23:01

标签: excel vba excel-vba row

我的数据集如下:

Original data

本质上,我需要删除一个重复的行(bar项目),并将项目移动到第一行和另一行的右侧。

的例子

end goal format

我对VBA的经验很少,对于从哪里开始的任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:0)

这应该是直截了当的,任何问题只是问

Public Sub MergeProjects()
Dim lastrow As Long
Dim lastcol As Long
Dim i As Long

    Application.ScreenUpdating = False

    With ActiveSheet

        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For i = lastrow - 1 To 2 Step -1

            If .Cells(i + 1, "A").Value = .Cells(i, "A").Value And _
                .Cells(i + 1, "B").Value = .Cells(i, "B").Value And _
                .Cells(i + 1, "C").Value = .Cells(i, "C").Value And _
                .Cells(i + 1, "D").Value = .Cells(i, "D").Value And _
                .Cells(i + 1, "E").Value = .Cells(i, "E").Value Then

                lastcol = .Cells(i, "A").End(xlToRight).Column
                .Cells(i + 1, "F").Resize(, 100).Copy .Cells(i, lastcol + 1)
                .Rows(i + 1).Delete
            End If
        Next i

        lastcol = .Range("A1").CurrentRegion.Columns.Count
        .Range("F1:G1").Value = Array("Project 1", "Project 2")
        If lastcol > 7 Then

            .Range("F1:G1").AutoFill .Range("F1").Resize(, lastcol - 5)
        End If
    End With

    Application.ScreenUpdating = True
End Sub