如何将字符串拆分为多个单元格的单元格?

SWi*_*WiM 2 excel vba excel-vba

我希望我的代码通过一个包含名称的单元格列表,然后将它们拆分为原始单元格旁边的单元格.我有一些基本的代码来做第一位,但是我很难让它在我的列表的其余部分循环,并且还将它输出到原始而不是A1当前的A1.我认为这是代码中"Cell"部分的一个问题,但我无法解决它.

Sub NameSplit()

    Dim txt As String
    Dim i As Integer
    Dim FullName As Variant
    Dim x As String, cell As Range

    txt = ActiveCell.Value

    FullName = Split(txt, " ")

    For i = 0 To UBound(FullName)

        Cells(1, i + 1).Value = FullName(i)

    Next i


End Sub
Run Code Online (Sandbox Code Playgroud)

RGA*_*RGA 6

在名称值范围上使用For Each循环.在这种情况下,我只是假设他们在第一列,但你可以相应地调整:

Sub NameSplit()

Dim txt As String
Dim i As Integer
Dim FullName As Variant
Dim x As String, cell As Range

For Each cell In ActiveSheet.Range(Cells(1,1),Cells(ActiveSheet.UsedRange.Count,1))
     txt = cell.Value

     FullName = Split(txt, " ")

     For i = 0 To UBound(FullName)

         cell.offset(0,i + 1).Value = FullName(i)

     Next i

Next cell

End Sub
Run Code Online (Sandbox Code Playgroud)