excel VBA合并单元格,保留单元格内容
(将合并单元格中的数据也合并)
比如这是你要合并两个单元格里的数据,做法如下
A B C
255 8146
在C1单元格里输入=A1&""&B1
注意,在两个双引号里不能有空格,否则就会变成 255 8146
(多元合并,且保留多元数据)
Sub 合并同类项()
If Selection.Columns.Count > 1 Then MsgBox "只能对单列操作,请重新选择区域!": Exit Sub
Selection.Offset(0, 1).EntireColumn.Insert
With Selection
For i = .Cells.Count To 2 Step -1
If .Cells(i) = .Cells(i - 1) Then Range(.Cells(i).Offset(0, 1), .Cells(i - 1).Offset(0, 1)).Merge
Next
Selection.Offset(0, 1).Copy
.PasteSpecial xlPasteFormats
.Offset(0, 1).EntireColumn.Delete
End With
End Sub
3、
代码如下:
Sub aaa()
For i = 2 To Range("L65536").End(xlUp).Row
If Cells(i, "K") <> "" Then
j = i
Else
Cells(j, "M") = Cells(j, "M") & Chr(10) & Cells(i, "M")
Cells(j, "L") = Cells(j, "L") & Chr(10) & Cells(i, "L")
Application.DisplayAlerts = False
For r = 1 To 13
Range(Cells(j, r), Cells(i, r)).Merge
Next
Application.DisplayAlerts = True
End If
Next
End Sub