在 Excel 中拆分儲存格(完整指南,詳細步驟)

在 Excel 中拆分儲存格(完整指南,詳細步驟)

使用 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:運行程式碼按下 F5 鍵以運行程式碼。然後您需要進行以下配置。

將出現一個對話框,提示您選擇包含要拆分數據的儲存格(這裡我選擇範圍 A2:A4)。做出選擇後,點擊「確定」。 在第二個彈出的對話框中,您需要選擇輸出範圍(這裡我選擇 B6 儲存格),然後點擊「確定」。 在最後一個對話框中,輸入用於拆分儲存格內容的分隔符(這裡我輸入斜線),然後點擊「確定」按鈕。 結果所選範圍內的儲存格同時被拆分為多行。

相关推荐

京东外卖app骑手注册,快来收藏!
365比分官网

京东外卖app骑手注册,快来收藏!

📅 07-10 👁️ 2599
怎么在浏览器下载视频
365365bet

怎么在浏览器下载视频

📅 07-05 👁️ 2971
每只企鹅每天能吃多少食物?全世界有多少种企鹅?
优酷、爱奇艺、腾讯视频哪个好?三大视频平台全方位对比
智能家居
日博365怎么样

智能家居

📅 08-23 👁️ 2708
2024年你应该尝试的 7 个最佳直播带货平台
日博365怎么样

2024年你应该尝试的 7 个最佳直播带货平台

📅 07-08 👁️ 1781
微信群发助手位置介绍
365比分官网

微信群发助手位置介绍

📅 08-19 👁️ 2229
RabbitMQ出现服务启动几秒退出问题
365365bet

RabbitMQ出现服务启动几秒退出问题

📅 08-10 👁️ 5204
小组第一!摩洛哥晋级世界杯16强,非洲两队已出线,压倒亚洲