我想在 vba 中编写一个代码,将单元格中的数组拆分为多行

我想在 vba 中编写一个代码,将单元格中的数组拆分为多行

我正在尝试将数据从单个单元格拆分为多行,如下所示。

[上面是之前,下面是之后

我正在使用下面提到的代码:

Sub TransposeRange()

Dim rng As Range
Dim InputRng As Range, OutRng As Range
xTitleId = "Splitcell"
Set InputRng = Application.Selection.Range("A1")
Set InputRng = Application.InputBox("Range(single cell) :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
Arr = VBA.Split(InputRng.Range("A1").Value, Chr(9))
OutRng.Resize(UBound(Arr) - LBound(Arr) + 1).Value = Application.Transpose(Arr)
End Sub

答案1

确保您的输入单元格有水平制表符。您的代码:

Sub TransposeRange()
    Dim rng As Range
    Dim InputRng As Range, OutRng As Range

    xTitleId = "Splitcell"
    Set InputRng = Application.Selection.Range("A1")
    Set InputRng = Application.InputBox("Range(single cell) :", xTitleId, InputRng.Address, Type:=8)
    Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)

    Arr = VBA.Split(InputRng.Range("A1").Value, Chr(9))
    OutRng.Resize(UBound(Arr) - LBound(Arr) + 1).Value = Application.Transpose(Arr)
End Sub

典型结果:

在此处输入图片描述

相关内容