尝试将数据从一个单元格拆分为多个单元格

时间:2019-06-10 04:15:16

标签: excel vba

我正在尝试将数据从一个单元格拆分为多个单元格。数据示例行为:

7/21 6.98 2.18 CR 7 / 19-7 / 24

我的目标是通过VBA宏使每个数据都出现在单独的列中。这是我到目前为止的代码

Sub nameTest()

    Dim txt As String
    Dim i As Integer
    Dim FullName As Variant

    txt = ActiveCell.Value

    FullName = Split(txt, " ")
    For i = 0 To UBound(FullName)
        Cells(1, i + 1).Value = FullName(i)
    Next i

End Sub

我的问题是我想不出一种方法来使它运行在每一行而不是每一行。有谁知道如何做到这一点?谢谢您的时间和帮助

2 个答案:

答案 0 :(得分:2)

假设B:F列可用,也许:

├───myscript.py
├───env

答案 1 :(得分:0)

您可以尝试:

代码:

Option Explicit

'Method 1 - Fixed range
Sub nameTest()

    With ThisWorkbook.Worksheets("Sheet1")

        .Range("C1:G1").Value = Split(.Range("A1"), " ")

    End With

End Sub

'Method 2 - Dinamic range starting from C1
Sub nameTest()

    Dim arr As Variant

    With ThisWorkbook.Worksheets("Sheet1")

        arr = Split(.Range("A1"), " ")

        .Range(.Cells(1, 3), .Cells(1, UBound(arr) + 3)).Value = arr

    End With

End Sub

结果:

enter image description here

相关问题