Creating a jagged array in VBA, writing seems to work but reading fails Type Mismatch -
i'm in sap, trying dump array of guilabel s jagged array.
first parse child's id row , column while filtering out non-guilabel objects.
this part works.
i created variant array represents lines, in each line element put array of strings representing each 'label columns'.
this part seems work no errors
the problem occurs when try read first variant array.
i followed great thread -> how set "jagged array" in vba?
and end of it, seems should able read each of elements simple debug.print myrows(0)(0) , type mismatch !
any advice appreciated !
function labeltoarray(lblcollection variant) variant() dim myid string dim mycolumn string dim myrow string dim intlastrow integer dim intlastcol integer dim myrows variant dim mycolumns() string ' create first row redim myrows(0) ' every child object in collection each mychld in lblcollection ' execute labels if mychld.type = "guilabel" ' column , row of current label myid = split(mychld.id, "[")(4) myid = mid(myid, 1, len(myid) - 1) mycolumn = split(myid, ",")(0) myrow = split(myid, ",")(1) ' new row ? reset column counter, set row counter , redim array of rows ' fail spectacularly if sap stop giving rows in numerical order if myrow > intlastrow intlastcol = 0: intlastrow = myrow: redim myrows(myrow) ' reset or resize line's columns array if intlastcol = 0 ' set filled out 'mycolumns' array correct 'myrows' object if myrow <> 0 myrows(myrow - 1) = mycolumns ' testing, print last column in row if myrow <> 0 debug.print mycolumns(ubound(mycolumns)) ' testing, print first column in row if myrow <> 0 debug.print mycolumns(0) ' testing, print number of columns if myrow <> 0 debug.print ubound(mycolumns) ' reset mycolumns array because we're on new line redim mycolumns(0) else redim preserve mycolumns(intlastcol) end if mycolumns(intlastcol) = mychld.text intlastcol = intlastcol + 1 'debug.print mycolumn & "," & myrow end if next ' copy last row array myrows(myrow) = mycolumns ' seems work fine point ! debug.print ubound(myrows) debug.print myrows(0)(0) 'this line fails, type mismatch (run-time error 13) ! tried cstr(myrows(0)(0)) labeltoarray = myrows end function
that error thrown if second dimension not exist. money go on problem.
here's example of array of jagged arrays, last being empty. last line throw type mismatch error because there no second dimension.
dim mainarray(0 3) variant dim subarray variant subarray = array("a", "b", "c") mainarray(0) = subarray debug.print mainarray(0)(0) 'prints subarray = array(1, 2, 3, 4) mainarray(1) = subarray debug.print mainarray(1)(0) 'prints 1 subarray = split("hello|world", "|") mainarray(2) = subarray debug.print mainarray(2)(0) 'prints hello mainarray(3) = empty debug.print mainarray(3)(0) 'type mismatch
have in locals window , test if arrays correct.
as noted in comments, redim
without preserve
looks suspicious - think may clearing old arrays.
Comments
Post a Comment