Excel中如何用VBA实现:当第一列的单元格部分内容相同时,则合并对应第二列单元格

详见如图,第二列单元格中钱六个字符均相同,则对应合并第一列的单元格。

按Alt+F11,打开VBA编辑器,插入一个模块,把下面的代码贴进去,按F5执行

Sub 宏1()
lastrow = Cells(Rows.Count, 2).End(3).Row
i = 1
k = 1
Do While i <= lastrow
    If Left(Cells(i, 2), 6) = Left(Cells(i + 1, 2), 6) Then
        i = i + 1
    Else
        Range(Cells(k, 1), Cells(i, 1)).Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.Merge
        i = i + 1
        k = i
    End If
Loop
End Sub

测试通过。

温馨提示:答案为网友推荐,仅供参考
相似回答