我想将逗号分隔值(或任何分隔符)转换为数组中的多个行。我看到的大多数示例都保存在excel文件中的范围值中。 可以请任何建议。
strFirstLine:
row1,row2,row3
在数组中输出,所以我可以循环每一行进行操作:
row1
row2
row3
这是我到目前为止的代码。我在debug.print部分添加了注释。
line = WorksheetFunction.Transpose(Split(strFirstLine, ","))
ctr = 0
For ctr = LBound(line) To UBound(line)
Debug.Print LBound(line) --value is 1
Debug.Print UBound(line) --value is 13
Debug.Print CStr(line(LBound(line))) --this line fails with "Subscript out of Range" error
Next ctr
如果您需要更多详细信息,请与我们联系。任何想法都将不胜感激!
答案 0 :(得分:1)
line
这里是一个二维数组,因此您需要在使用LBound
或UBound
时指定维度,并且在访问值时需要两个索引:
line = WorksheetFunction.Transpose(Split("row1,row2,row3", ","))
Debug.Print LBound(line, 1), UBound(line, 1) '1,3
Debug.Print LBound(line, 2), UBound(line, 2) '1,1
For ctr = LBound(line, 1) To UBound(line, 1)
Debug.Print line(ctr, 1)
Next ctr
答案 1 :(得分:1)
您可以避免转置功能。只需使用拆分并定义要保存拆分值及其完成的行和列值。
检查此代码:
Sub try()
Dim Line() As String
Dim ctr, rw, cl As Integer
strFirstLine = "row1,row2,row3"
Line = Split(strFirstLine, ",")
ctr = 0
rw = 1
cl = 1
For ctr = LBound(Line) To UBound(Line)
Cells(rw, cl) = Line(ctr)
rw = rw + 1
Next ctr
End Sub