Excel宏開發之合並單元格


合並單元格

Sub 宏1()
'
' 宏1 宏
'
' 快捷鍵: Ctrl+q
'
    Application.Goto Reference:="宏1"
    Application.VBE.MainWindow.Visible = True
    
    Application.DisplayAlerts = False
    
    For i = [A65536].End(3).Row To 2 Step -1
        If Cells(i - 1, 1) = Cells(i, 1) Then
            Cells(i - 1, 1).HorizontalAlignment = xlCenter
            Cells(i - 1, 1).VerticalAlignment = xlCenter
            Range(Cells(i - 1, 1), Cells(i, 1)).Merge
        End If
    Next
    
    For i = [B65536].End(3).Row To 2 Step -1
        If Cells(i - 1, 2) = Cells(i, 2) Then
             flag = True
             If (Cells(i, 1) <> "") Then
                flag = False
             End If
             If flag Then
                Cells(i - 1, 2).HorizontalAlignment = xlCenter
                Cells(i - 1, 2).VerticalAlignment = xlCenter
                Range(Cells(i - 1, 2), Cells(i, 2)).Merge
             End If
        End If
    Next
    
     For i = [C65536].End(3).Row To 2 Step -1
        If Cells(i - 1, 3) = Cells(i, 3) Then
             flag = True
             If (Cells(i, 2) <> "") Then
                flag = False
             End If
             If flag Then
                Cells(i - 1, 3).HorizontalAlignment = xlCenter
                Cells(i - 1, 3).VerticalAlignment = xlCenter
                Range(Cells(i - 1, 3), Cells(i, 3)).Merge
             End If
             
        End If
    Next

    Application.DisplayAlerts = True
    
    Application.VBE.MainWindow.Visible = False
End Sub

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM