591游戏城 - 最新网游活动资讯聚合平台
首页活动中心正文

在 Excel 中分割单元格(详细步骤的完整指南)

2025-10-17 07:22:05

使用 VBA代码将单元格分割为多行本节为您提供了一个 VBA代码,可以轻松地将单元格分割为 Excel 中的多行。请按以下步骤操作。

步骤1:打开 Microsoft Visual Basic for Applications 窗口按 Alt + F11 键打开此窗口。

步骤2:插入模块并输入 VBA代码点击“插入”>“模块”,然后将以下 VBA代码复制并粘贴到模块(代码)窗口中。

VBA代码:在 Excel 中将单元格分割为多行

Option Explicit

Sub SplitCellsToRows()

'Updated by Extendoffice 20230727

Dim inputRng As Range

Dim outputRng As Range

Dim cell As Range

Dim splitValues() As String

Dim delimiter As String

Dim i As Long

Dim columnOffset As Long

On Error Resume Next

Set inputRng = Application.InputBox("Please select the input range", "Kutools for Excel", Type:=8) ' Ask user to select input range

If inputRng Is Nothing Then Exit Sub ' If the user clicked Cancel or entered nothing, exit the sub

Set outputRng = Application.InputBox("Please select the output range", "Kutools for Excel", Type:=8) ' Ask user to select output range

If outputRng Is Nothing Then Exit Sub ' If the user clicked Cancel or entered nothing, exit the sub

delimiter = Application.InputBox("Please enter the delimiter to split the cell contents", "Kutools for Excel", Type:=2) ' Ask user for delimiter

If delimiter = "" Then Exit Sub ' If the user clicked Cancel or entered nothing, exit the sub

If delimiter = "" Or delimiter = "False" Then Exit Sub ' If the user clicked Cancel or entered nothing, exit the sub

Application.ScreenUpdating = False

columnOffset = 0

For Each cell In inputRng

If InStr(cell.Value, delimiter) > 0 Then

splitValues = Split(cell.Value, delimiter)

For i = LBound(splitValues) To UBound(splitValues)

outputRng.Offset(i, columnOffset).Value = splitValues(i)

Next i

columnOffset = columnOffset + 1

Else

outputRng.Offset(0, columnOffset).Value = cell.Value

columnOffset = columnOffset + 1

End If

Next cell

Application.ScreenUpdating = True

End Sub步骤3:运行 VBA代码按 F5 键运行代码。然后您需要进行以下配置。

将出现一个对话框,提示您选择要分割数据的单元格(这里我选择区域 A2:A4)。选择后,点击 确定. 在第二个弹出的对话框中,您需要选择输出区域(这里我选择单元格 B6),然后点击 确定. 在最后一个对话框中,输入用于分割单元格内容的分隔符(这里我输入斜杠),然后点击 确定 按钮。 结果选定范围内的单元格同时被分割为多行。

迪丽热巴生肖与珠宝神图解析 为什么家用消毒柜只有中国流行?而国外不怎么流行?
相关内容