使用此循环分割脚本需要帮助定义范围和string

我有列C中的数据列表,我正在寻找把这个分割函数的输出放到列D中。到目前为止我写的脚本是:

Sub SplitTest() Dim txt As String Dim Rng As Range Dim i As Integer Dim Authors As Variant i = 2 Set Rng = Range("C" & i) txt = Rng.Value Authors = Split(txt, ["."]) For i = 2 To UBound(Authors) Set Rng = Range("C" & i) Range("D" & i + 1).Value = Authors(i) Next i End Sub 

现在,脚本只能运行3到5行Range("D"...行吗?

提前致谢

您需要先设置范围,然后才能使用它。

 Dim Rng As Range Set Rng = Range("C" & i) txt = Rng.Value 

另外,你需要设置i =一些行号才能使用它。 你现在有

 Dim i As Integer Dim Authors As Variant Set Rng = Range("C" & i) 

但是我还没有设定。 你需要设置它。

 Dim i As Integer Dim Authors As Variant i = 1 Set Rng = Range("C" & i) 

编辑:每OP评论。

看看把所有的东西放在一个循环上。

 Sub SplitTest() Dim ws As Excel.Worksheet Dim lRow As Long Dim txt As String Dim Rng As Range Dim i As Integer Dim Authors As Variant Dim strColumn As String Set ws = Application.ActiveSheet lRow = 1 'Loop through process each row. Do While lRow <= ws.UsedRange.Rows.count Set Rng = Range("C" & lRow) txt = Rng.Value Authors = Split(txt, ["."]) 'Loop through the split results and put them in column to the right For i = 0 To UBound(Authors) strColumn = Col_Letter(i + 4) ws.Range(strColumn & lRow) = Authors(i) Next i lRow = lRow + 1 Loop End Sub 

添加这个function

 Function Col_Letter(lngCol As Long) As String Dim vArr vArr = Split(Cells(1, lngCol).Address(True, False), "$") Col_Letter = vArr(0) End Function 

在你的情况下:

 i = 2 '<--- Some integer value Set Rng = Range("C" & i) txt = Rng.Value Authors = Split(txt, ["."])