我在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"...
行有问题吗?
提前致谢
答案 0 :(得分:2)
您需要先设置范围才能使用它。
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 Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
答案 1 :(得分:1)
在你的情况下:
i = 2 '<--- Some integer value
Set Rng = Range("C" & i)
txt = Rng.Value
Authors = Split(txt, ["."])