redim returned split function variable

  • why can't you redim the output from a split function?

    (the output of the split function is an array)


    eg:

    Code
    sv = Split(lnarr, ",", 13, 1)
    ReDim Preserve sv(UBound(sv)-1)

    Fails with invaild redim


    I did find a workaround (But I still need to know why)


    the workaround:


    Code
    Dim TJ() As String
      sv = Split(lnarr, ",", 13, 1)
      ReDim Preserve TJ(UBound(sv))
      TJ = sv
      ReDim Preserve TJ(UBound(sv) - 1)
  • sorry error in my post


    here's the correction

    Old:

    Code
    ReDim Preserve TJ(UBound(sv) - 1)

    New:

    Code
    ReDim Preserve TJ(UBound(TJ) - 1)
  • Your first method works fine for me, I tested as follows with the debug.print window showing 5 then 4 as expected and the watch window for the array losing "purple" after the redim. Not sure why it is not working for you:


  • I figured it out.


    I see you actually define myArr (dim myArr)

    I dont dim the split variable, since when splitting it auto defines it as an array


    when i define it, it works just fine


    Thanks

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!