Sub 合併()
Dim T$, xR As Range, xH As Range
Application.DisplayAlerts = False
For Each xR In Range([A2], [A65536].End(xlUp))
If xR <> T Then Set xH = xR: T = xR
If xR(2) <> T Then Range(xH, xR).Merge
Next
End Sub作者: gaishutsusuru 時間: 2020-5-9 11:43
Sub 合併()
Dim R&, i&, xA As Range, xZ As Range, T$, xH As Range
R = [A65536].End(xlUp).Row
Set xA = Range("A2:A" & R)
Set xZ = Range("Z2:Z" & R) '借用z欄當合併格
xA.Copy: xZ.PasteSpecial xlPasteFormats '複製格式到z欄
For i = 1 To xA.Count
If xA(i) <> T Then Set xH = xZ(i): T = xA(i)
If xA(i + 1) <> T Then Range(xH, xZ(i)).Merge
Next i
xZ.Copy: xA.PasteSpecial xlPasteFormats '複製格式到a欄
xA.Borders.LineStyle = 1 '加框線, 解除合併後仍保有框線
xZ.EntireColumn.Delete '清除z欄
End Sub