VBA合并相同单元格

 

vba即使在2023年,依然强大和效率极高。

Sub 合并()
        Call 合并1
        Call 合并2
        Call 合并3
        Call 合并4
End Sub


Sub 合并1()
        Dim j As Integer
        Dim i As Integer

        Application.DisplayAlerts = False
        j = [A65536].End(xlUp).Row
        For i = j To 2 Step -1
                If Cells(i, 1) = Cells(i - 1, 1) Then
                        Range(Cells(i - 1, 1), Cells(i, 1)).Merge
                End If
        Next
End Sub

Sub 合并2()
        Dim j As Integer
        Dim i As Integer

        Application.DisplayAlerts = False
        j = [A65536].End(xlUp).Row
        For i = j To 2 Step -1
                If Cells(i, 2) = Cells(i - 1, 2) Then
                        Range(Cells(i - 1, 2), Cells(i, 2)).Merge
                End If
        Next
End Sub

 

Sub 合并3()
        Dim j As Integer
        Dim i As Integer

        Application.DisplayAlerts = False
        j = [A65536].End(xlUp).Row
        For i = j To 2 Step -1
                If Cells(i, 3) = Cells(i - 1, 3) Then
                        Range(Cells(i - 1, 3), Cells(i, 3)).Merge
                End If
        Next
End Sub

 

Sub 合并4()
        Dim j As Integer
        Dim i As Integer

        Application.DisplayAlerts = False
        j = [A65536].End(xlUp).Row
        For i = j To 2 Step -1
                If Cells(i, 4) = Cells(i - 1, 4) Then
                        Range(Cells(i - 1, 4), Cells(i, 4)).Merge
                End If
        Next
End Sub

posted @   McKay  阅读(309)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
点击右上角即可分享
微信分享提示