I would like to transpose comma separated values (or any delimiter) to multiple rows in an array. Most of the examples I'm seeing are saved to a range value in excel file. Could any please advise.
strFirstLine:
row1,row2,row3
Ouput in Array so I could loop on each line for manipulation:
row1
row2
row3
Here is the code I have so far. I have added comment on the debug.print part.
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
Let me know if you need more details. Any thoughts will be appreciated!
line
here is a 2-d array, so you need to specify the dimension when using LBound
or UBound
, and you need both indexes when accessing a value:
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
You can avoid transpose function. Simply use split and define row and column value where you want to save the split values and its done.
Check this code:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.