vbscript
Sub 合并多个单元格并保留数据()
Dim rng As Range
Dim val As String
Dim cell As Range
Set rng = Selection
If rng.Cells.Count <= 1 Then
Exit Sub
End If
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each cell In rng
If cell.Value <> "" Then
val = val & cell.Value & vbNewLine
End If
Next cell
With rng
.Merge
.Value = Trim(val)
.WrapText = True
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
在excel中可以启用宏【alt+F11】新建模块,选中所需单元格【alt+F8】完成多列单元格合并操作.
根据需求更改了 val = val & cell.Value & vbNewLine,合并后保持一列一换行的格式。