VBA: Strings and arrays - splitting 1d arrays into 2d arrays -


i've got string array containing string of items separated "."

ex.:

0: test1.test2.test3.test4.test5 1: testa.testb.testc.testd.teste ... 

i need split these strings 2d string array 5 elements per array.

ex.:

0, 0: test1 0, 1: test2 ... 1, 0: testa 1, 1: testb ... 

i'm struggling bit, since isn't quite c or c#.

when testing , trying, works:

nb: testlist array first example, containing entries of "."-separated strings. it's created this, long string called strarray:

testlist = split(textline, "<")       dim temp() string temp = split(testlist(0), ".") msgbox join(temp, vbcrlf) 

above, create new 1d string array temp() , split first index of string array testlist that. works fine.

however, when try create 2d string array , split first index of testlist string array first index of that, not work:

dim indtestslist() string               'new string array redim indtestslist(arrsize, 5)             'initialize 2d size 'msgbox "arrsize " & arrsize indtestslist(0) = split(testlist(0), ".")  'split first array 'msgbox join(indtestslist(0), vbcrlf) 

this gives error

type mismatch 

so, seems obvious i've not declared or created 2d array correctly, or i'm trying insert wrong wrong place. cannot see what/where?

however, if insert strings 2d array instead of splitting, works:

dim indtestslist() string redim indtestslist(arrsize, 5) indtestslist(0, 0) = "dritt" indtestslist(0, 1) = "piss" msgbox indtestslist(0, 0) msgbox indtestslist(0, 1)  'however, not work. why? subscript out of range... msgbox join(indtestslist(0), vbcrlf)  

so seems able create 2d string array , populate in simple way. not beyond that.

sorry not getting vba syntaxes , standards here, i've been banging head against while. there might on web, of course, i've found hasn't been needed. appreciated here, , constructive answers of course credited.

(final) update

there's lots of advice below. struggling bit, excelinefendisi put me on track working, simple solution. i'm posting solution chose here, , crediting answer him.

dim intcount        integer dim intcount2       integer dim temparray1d()   string dim finalarray2d()  string  ...  'resize array holding final ordered sets of tests: redim finalarray2d(arrsize, 5)  'loop through testlist() array , split each array 2d  'finalarray2d: intcount = lbound(testlist) ubound(testlist) - 2     'split first line in testlist() temparray1d():     temparray1d = split(testlist(intcount), ".")     'copy elements temparray1d() current x index of      'temparray2d():     intcount2 = 0 4         finalarray2d(intcount, intcount2) = temparray1d(intcount2)     next intcount2 next intcount 

i've removed other updates, think developed more noise , confusion necessary. answered , helped.

this code do

sub macro3() dim testlist(0 1) string dim temp1 variant dim temp2 variant dim resultarray(0 1, 0 4) string  testlist(0) = "test1.test2.test3.test4.test5" testlist(1) = "testa.testb.testc.testd.teste"   temp1 = split(testlist(0), ".") temp2 = split(testlist(1), ".")   = 0 1 'actually ubound(testlist)     j = 0 4 'actually ubound(temp, 2)         resultarray(i, j) = iif(i mod 2 = 0, temp1(j), temp2(j))     next j next  msgbox resultarray(0, 2)  end sub 

if need more dynamic version, use collections instead of temporary variant arrays below

sub macro4() dim testlist(0 1) string dim temp new collection  dim resultarray(0 1, 0 4) string  testlist(0) = "test1.test2.test3.test4.test5" testlist(1) = "testa.testb.testc.testd.teste"  = 0 ubound(testlist)     temp.add (split(testlist(i), ".")) next   = 0 ubound(testlist)     j = 0 ubound(resultarray, 2)         resultarray(i, j) = iif(i mod 2 = 0, temp(i + 1)(j), temp(i + 1)(j))     next j next  msgbox resultarray(0, 2)  end sub 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -