excel利用vba自动合并相同单元格?

如图所示,想要对E列中相邻的有相同值的单元格进行合并,能实现吗?请问代码要怎么写?

就你目前的截图,代码如下:

Sub test()

Dim i, irow As Integer

irow = Range("e65536").End(xlUp).Row

Application.ScreenUpdating = False

Application.DisplayAlerts = False

For i = irow To 7 Step -1

    If Cells(i, 5) <> "" And Cells(i + 1, 5) <> "" And Cells(i, 5) = Cells(i + 1, 5) Then

        Range(Cells(i + 1, 5), Cells(i, 5)).Merge

    End If

Next

Range(Cells(6, 5), Cells(irow, 5)).HorizontalAlignment = xlCenter

Range(Cells(6, 5), Cells(irow, 5)).VerticalAlignment = xlCenter 

Application.ScreenUpdating = True

Application.DisplayAlerts = True

End Sub

温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-08-22

万能处理

Excel数据重复或+空格怎样批量合并单元格

第2个回答  2021-08-21
Sub hb()
Dim i&, r&, rend&, c As Range
Application.DisplayAlerts = False
rend = Cells(Rows.Count, 5).End(3).Row
For i = 7 To rend
Set c = Cells(i, 5)
If c(0) <> c And c <> "" Then r = i
If c(2) <> c And c <> "" And i <> r Then
Range(Cells(r, 5), c).Merge
End If
Next
Application.DisplayAlerts = True
End Sub
第3个回答  2021-08-21
为什么要用VBA呢?用分类汇总、定位合并单元格不一样能解决这个问题?
相似回答