I've got a String array containing a string of items separated by "."
Ex.:
0: Test1.Test2.Test3.Test4.Test5
1: Testa.Testb.Testc.Testd.Teste
...
I need to split these strings into a 2D String array with 5 elements per array.
Ex.:
0, 0: Test1
0, 1: Test2
...
1, 0: Testa
1, 1: Testb
...
I'm struggling a bit, since this isn't quite like C or C#.
When testing and trying, this works:
NB: testList is an array like the first example, containing entries of "."-separated strings. It's created like this, from a long string called strArray:
testList = Split(textline, "<")
Dim temp() As String
temp = Split(testList(0), ".")
MsgBox Join(temp, vbCrLf)
Above, I just create a new 1D String array temp() and split the first index of the String array testList into that. Works fine.
However, when I try to create a 2D String array and split the first index of the testList String array into the first index of that, it does NOT work:
Dim indTestsList() As String 'New String array
ReDim indTestsList(arrSize, 5) 'Initialize the 2D size
'MsgBox "arrSize " & arrSize
indTestsList(0) = Split(testList(0), ".") 'Split into the first array
'MsgBox Join(indTestsList(0), vbCrLf)
This gives the error
Type mismatch
So, it seems obvious that I've not declared or created the 2D array correctly, or I'm trying to insert something wrong into the wrong place. I just cannot see what/where?
However, if I just insert strings into the 2D array instead of splitting, it works:
Dim indTestsList() As String
ReDim indTestsList(arrSize, 5)
indTestsList(0, 0) = "dritt"
indTestsList(0, 1) = "piss"
MsgBox indTestsList(0, 0)
MsgBox indTestsList(0, 1)
'However, this does not work. Why? Subscript out of range...
MsgBox Join(indTestsList(0), vbCrLf)
So it seems that I AM able to create a 2D String array and populate it in the most simple way. Just not beyond that.
Sorry for not getting the VBA syntaxes and standards here, but I've been banging my head against this for a while. There might be help for this on the web, of course, but what I've found about this hasn't been what I needed. Any help is appreciated here, and all constructive answers will of course get credited.
(FINAL) UPDATE
There's lots of good advice below. I was struggling a bit, but ExcelinEfendisi put me on the track to a working, simple solution. I'm posting the solution I chose here, and crediting the answer to him.
Dim intCount As Integer
Dim intCount2 As Integer
Dim tempArray1d() As String
Dim finalArray2d() As String
...
'Resize the array for holding the final ordered sets of tests:
ReDim finalArray2d(arrSize, 5)
'Loop through the testList() array and Split each array into the 2d
'finalArray2d:
For intCount = LBound(testList) To UBound(testList) - 2
'Split the first line in testList() into tempArray1d():
tempArray1d = Split(testList(intCount), ".")
'Copy the elements from tempArray1d() to the current x index of
'tempArray2d():
For intCount2 = 0 To 4
finalArray2d(intCount, intCount2) = tempArray1d(intCount2)
Next intCount2
Next intCount
I've removed the other updates, as I think they developed into more noise and confusion than necessary. Thanks to those who answered and helped.