1. 程式人生 > >VBA 第9課 合併單元格

VBA 第9課 合併單元格

'單元格合併
Sub b1()
Range("a1:g2").Merge
End Sub

'合併H列相同單元格
Sub b2()
Dim x As Integer
Dim rg As range
Set rg = range("h1")
Application.DisplayAlerts = False
For x = 1 To 13
    If range("h" & x + 1) = range("h" & x) Then
        Set rg = Union(rg, range("h" & x + 1))
    Else
    rg.Merge
    Set rg = range("h" & x + 1)
    End If
Next x
Application.DisplayAlerts = True
End Sub