假设我们有许多单元格需要与文本合并在一起。 Excel 标准集具有类似的功能:“合并并居中”,但在执行合并时,它只留下最外层单元格的文本。所以我们需要使用我们自己的宏来达到这样的目的。
为此,请打开编辑器 Visual Basic (Alt+F11)、insert VBA module (Insert - Module) 并复制该函数的文本:
Sub MergeCell() Const sDELIM As String = " " Dim rCell As Range Dim sMergeStr As String If TypeName(Selection) <> "Range" Then Exit Sub With Selection For Each rCell In .Cells sMergeStr = sMergeStr & sDELIM & rCell.Text Next rCell Application.DisplayAlerts = False .Merge Across:=False Application.DisplayAlerts = True .Item(1).Value = Mid(sMergeStr, 1 + Len(sDELIM)) End With End Sub
保存并返回Excel。
现在选择所需的单元格并运行我们的宏 (Alt+F8)(宏名称:“MergeCell”)。