新年祝福
各位读者:
大家新年好!
今天我要和各位分享的是:在我们日常工作中,经常会遇到内容相同的连续单元格需要合并的问题,那么如何快速的实现呢,又如何在合并后单元格格式会自动地靠左居中对齐呢?
下面的代码就可以一键解决这个问题。
Sub CombineSameCells()
Dim irow As Long, i As Long
Application.DisplayAlerts = False
With ActiveSheet
irow = ActiveSheet.Range('A1048576').End(xlUp).Row
For i = irow To 2 Step -1
If ActiveSheet.Cells(i, 1).Value = .Cells(i - 1, 1).Value Then
ActiveSheet.Range(.Cells(i - 1, 1), .Cells(i, 1)).Merge
ActiveSheet.Range(.Cells(i - 1, 1), .Cells(i, 1)).HorizontalAlignment = xlLeft
ActiveSheet.Range(.Cells(i - 1, 1), .Cells(i, 1)).VerticalAlignment = xlCenter
End If
Next i
End With
Application.DisplayAlerts = True
End Sub
在Thisworkbook模块里输入上述代码后,然后在活动工作表里插入一命令按钮,我们就可以轻松实现上述功能了。
读者们,怎么样,过去需要几分钟,甚至几十分钟的工作,现在只需轻轻点击一下按钮,问题就完美地解决了!
当然,上述代码只是以合并A列为例,要是其它列的数据,只需把A1048576改为目标列*1048576就可以了。
要是喜欢,请点赞分享哦....谢谢!
联系客服