【问题标题】:VBA macro to merge duplicate rows based on multiple values in a rowVBA宏基于一行中的多个值合并重复行
【发布时间】:2019-08-17 20:58:30
【问题描述】:

我有一个示例 MS Excel 表格:

我正在尝试编写一个允许我比较行的 VBA 宏,比较是使用多个单元格 (A2:E2) 完成的,其余单元格 (F2:I2) 将合并其值而不进行比较。我希望能够比较一行 - 单元格(A2:E2)到单元格(A3:E3),然后单元格(A2:E2)到单元格(A4:E4)......完成比较后它会合并重复项 - 这样单元格(Fx:Ix)也会合并。

最终效果如下:

到目前为止,我已经想出了这段代码,但运行它会使 Excel 崩溃。任何形式的建议将不胜感激。

提前致谢

Sub MergeDuplicateRows()

    Dim i As Long
    Dim j As Long
    Dim RowCount As Long

    Dim sameRows As Boolean

    sameRows = True
    RowCount = Rows.Count

    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    For i = 1 To Range("B" & RowCount).End(xlUp).Row
        For j = 1 To 5
            If StrComp(Cells(i, j), Cells(i + 1, j), vbTextCompare) Then
                sameRows = False
            End If
        Next j

        If sameRows Then
            Range(Cells(i, 1), Cells(i + 1, 1)).Merge
            Range(Cells(i, 2), Cells(i + 1, 2)).Merge
            Range(Cells(i, 3), Cells(i + 1, 3)).Merge
            Range(Cells(i, 4), Cells(i + 1, 4)).Merge
            Range(Cells(i, 5), Cells(i + 1, 5)).Merge
            Range(Cells(i, 6), Cells(i + 1, 6)).Merge
            Range(Cells(i, 7), Cells(i + 1, 7)).Merge
            Range(Cells(i, 8), Cells(i + 1, 8)).Merge
            Range(Cells(i, 9), Cells(i + 1, 9)).Merge
        End If

        sameRows = True
    Next i

    Application.DisplayAlerts = True

End Sub

【问题讨论】:

  • 在你的最终结果中,为什么var8foo1 的行中有一个x(没有M)?
  • "...但运行它会导致 Excel 崩溃" - 这是什么意思?您收到的是实际的错误消息,还是 Excel 只是冻结?
  • excel 只是冻结并且永远不会唤醒 - 出现一个对话框,要求重新启动 MS Excel...
  • 最后一个问题 - 是否所有行都可能在彼此的顶部/下方匹配,或者它们可以在工作表上的任何位置?
  • 你也永远不会恢复ScreenUpdating

标签: excel vba


【解决方案1】:

试一试 - 我不得不改变一些逻辑,将您的 For 循环更改为 Do While 循环,而不是合并,我们只是删除行。我在您的示例数据上对此进行了测试,它运行良好,但我不确定它在 1500 行上的表现如何:

Sub MergeDuplicateRows()

    Dim i As Long
    Dim j As Long
    Dim sameRows As Boolean

    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    i = 2

    Do While Cells(i, 2).Value <> ""
        For j = 1 To 5
            If Cells(i, j).Value <> Cells(i + 1, j).Value Then
                sameRows = False
                Exit For
            Else
                sameRows = True
            End If
        Next j

        If sameRows Then
            If Cells(i, 6).Value = "" Then Cells(i, 6).Value = Cells(i + 1, 6).Value
            If Cells(i, 7).Value = "" Then Cells(i, 7).Value = Cells(i + 1, 7).Value
            If Cells(i, 8).Value = "" Then Cells(i, 8).Value = Cells(i + 1, 8).Value
            If Cells(i, 9).Value = "" Then Cells(i, 9).Value = Cells(i + 1, 9).Value

            Rows(i + 1).Delete
            i = i - 1
        End If

        sameRows = False
        i = i + 1
    Loop

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

End Sub

【讨论】:

  • 非常感谢 - 就像一个小数据集的魅力; ~1500 需要一段时间 - 至少我可以看到我的逻辑哪里出了问题:)。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2018-08-08
  • 2017-04-05
  • 2015-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多