合并重复的单元格?

Cap*_*oja 0 excel vba

我有以下输入:

输入项

并想要以下输出:

输出量

预期的操作是在A列中搜索重复值(列已排序)。A中的每个重复值应合并为1个单元格。另外,合并B中的相同行(如果不同,则取最大值,但可以放心地假定它们相同)。请勿触摸C。

我现在正在手动执行此操作,这非常痛苦。我是VBA的新手,但看来这是加快速度的简单方法。有小费吗?

new*_*240 5

Sub MergeCells()
    'set your data rows here
    Dim Rows As Integer: Rows = 20

    Dim First As Integer: First = 1
    Dim Last As Integer: Last = 0
    Dim Rng As Range

    Application.DisplayAlerts = False
    With ActiveSheet
        For i = 1 To Rows + 1
            If .Range("A" & i).Value <> .Range("A" & First).Value Then
                If i - 1 > First Then
                    Last = i - 1

                    Set Rng = .Range("A" & First, "A" & Last)
                    Rng.MergeCells = True
                    Set Rng = .Range("B" & First, "B" & Last)
                    Rng.MergeCells = True

                End If

                First = i
                Last = 0
            End If
        Next i
    End With
    Application.DisplayAlerts = True
End Sub
Run Code Online (Sandbox Code Playgroud)