如何取消合并单元格并保留单元格内容以及如何合并单元格
Sub 取消合并单元格并保留内容()
Dim strmer As String '用于存储需要取消合并单元格的内容
Dim intcot As Integer '用于存储被合并单元格的个数
Dim i As Integer, j As Integer '用于循环计数
Dim totalR As Integer, totalC As Integer '用于统计行数
Dim myrange As Range
Worksheets(1).Activate
totalR = Range("B65536").End(xlUp).Row
totalC = Range("IV2").End(xlToLeft).Column ‘由于最后一列首行也为合并单元格,故取第256列的第2行计算列号.
Debug.Print totalR, totalC
For j = 1 To totalC
For i = 2 To totalR
Set myrange = Range(Cells(i, j), Cells(i, j))
strmer = myrange.Value
intcot = myrange.MergeArea.Count
myrange.UnMerge
Range(Cells(i, j), Cells(i + intcot - 1, j)).Value = strmer
i = i + intcot - 1
Next i
Next j
Set myrange = Nothing
'去掉电话号码行,因为电话号码所在列的首行为空(虽然已经合并,但并不影响删除!!),所以利用这点检测到为空则将整列删除.
For j = totalC To 1 Step -1
If Range(Cells(1, j), Cells(1, j)).Value = "" Then
Range(Cells(1, j), Cells(1, j)).EntireColumn.Delete
End If
Next j
End Sub
合并单元格源程序如下:
Sub 合并单元格()
Dim introw As Integer, i As Integer, intcolumn As Integer, j As Integer
Application.DisplayAlerts = False
introw = Range("A65536").End(xlUp).Row
intcolumn = Range("IV1").End(xlToLeft).Column
Debug.Print intcolumn
For j = 1 To intcolumn
For i = introw To 2 Step -1
If Cells(i, j).Value = Cells(i - 1, j).Value Then
Range(Cells(i - 1, j), Cells(i, j)).Merge
End If
Next i
Next j
Application.DisplayAlerts = True
End Sub