Sub 合并多个单元格并保留数据()Dim rng As RangeDim val As StringDim cell As RangeSet rng = SelectionIf rng.Cells.Count <= 1 ThenExit SubEnd IfApplication.ScreenUpdating = FalseApplication.DisplayAlerts = FalseFor Each cell In rngIf cell.Value <> "" Thenval = val & cell.Value & vbNewLineEnd IfNext cellWith rng.Merge.Value = Trim(val).WrapText = TrueEnd WithApplication.ScreenUpdating = TrueApplication.DisplayAlerts = TrueEnd Sub
在excel中可以启用宏【alt+F11】新建模块,选中所需单元格【alt+F8】完成多列单元格合并操作.
参考:如何在excel中插入和运行VBA
Excel技巧:如何合并多个单元格而不丢失数据?
根据需求更改了 val = val & cell.Value & vbNewLine,合并后保持一列一换行的格式。