VBA 拆分字符串循环

Lea*_*ver 5 string excel vba split

我正在尝试拆分一个字符串并创建一个循环来遍历列中的单元格。有一些挑战:

  1. 拆分ActiveCell仅适用于。

  2. 循环遍历所有单元格,直到 LastRow,但ActiveCell仅使用拆分字符串值填充所有单元格。

  3. i = 0即使Option Base 1 在模块的开头有Array 的拆分也开始。

  4. 如何更改目标位置(例如,不是在现有数据旁边拆分字符串,是否有管理列号的选项)?

谢谢

Option Explicit
Option Base 1

Sub SplitStringLoop()

    Dim txt As String
    Dim i As Integer
    Dim y As Integer
    Dim FullName As Variant
    Dim LastRow As Single

    ReDim FullName(3)

    LastRow = Cells(Rows.Count, "A").End(xlUp).Row

    txt = ActiveCell.Value

    FullName = Split(txt, "-")

    For y = 2 To LastRow

            For i = 1 To UBound(FullName)

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

            Next i

   Next y

End Sub
Run Code Online (Sandbox Code Playgroud)

Row*_*anC 6

Chris Nelisen 概述了原因,我在他发布之前编写了这段代码,所以无论如何我都会发布它。

Option Explicit

Sub SplitStringLoop()

Dim txt As String
Dim i As Integer
Dim y As Integer
Dim FullName As Variant
Dim LastRow As Single

ReDim FullName(3)

LastRow = Cells(Rows.Count, "A").End(xlUp).Row

For y = 2 To LastRow
        Cells(y, 1).Select
        txt = ActiveCell.Value
        FullName = Split(txt, "-")
        For i = 0 To UBound(FullName)
           Cells(y, i + 2).Value = FullName(i)
        Next i
Next
End Sub
Run Code Online (Sandbox Code Playgroud)