有时候我们会遇到需要将相同的单元格合并起来的目的。
这时候可以使用如下的通用代码:
Sub exceloffice()
'作者QQ:1722187970,微信:xycgenius,微信公众号exceloffice
Excel.Application.ScreenUpdating = False
Excel.Application.DisplayAlerts = False
Set oDic = CreateObject("Scripting.Dictionary")
Dim oRng As Range
Dim oWK As Worksheet
Set oWK = Sheet1
With oWK
For i = 2 To .Range("a65536").End(xlUp).Row
sText = .Cells(i, "A")
If oDic.exists(sText) Then
Set oDic.Item(sText) = Excel.Application.Union(oDic.Item(sText), .Cells(i, "a"))
Else
oDic.Add sText, .Cells(i, "a")
End If
Next i
arrItems = oDic.items
For i = 0 To UBound(arrItems)
Set oRng = arrItems(i)
oRng.Merge
Next i
End With
Excel.Application.DisplayAlerts = True
Excel.Application.ScreenUpdating = True
End Sub


发表评论