在 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 儲存格),然後點擊「確定」。 在最後一個對話框中,輸入用於拆分儲存格內容的分隔符(這裡我輸入斜線),然後點擊「確定」按鈕。 結果所選範圍內的儲存格同時被拆分為多行。

相关推荐

招聘软件有哪些|哪个好用 我和大佬的365天第三季免费看

招聘软件有哪些|哪个好用

验证码在哪里找?教你快速找到验证码的方法 365bet官网网投

验证码在哪里找?教你快速找到验证码的方法

PlayStation VR 365bet官网网投

PlayStation VR