Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Variant Array to String

Tags:

vba

variant

I am trying to take a variant variable and convert it into a string so that I can run a split function on the data. However, whenever I try to redefine the variant I get a type mismatch error. I have used the CStr(), Str(), and ToString functions. None work.

Anything I am missing?

Function FlatLine(ByVal lines As Variant)

Dim flat() As String
ReDim Preserve flat(i)

For i = 0 To UBound(lines)
    flat(UBound(flat)) = lines(i)
    ReDim Preserve flat(LBound(flat) To UBound(flat) + 1)
Next i

Dim flat2 as String
flat2 = Cstr(flat)

^ errors there.
like image 974
user3428722 Avatar asked Mar 17 '14 13:03

user3428722


People also ask

What is a variant array?

An array is just a data structure that allows you to store information in a matrix like construct. It echos the organization of a worksheet in that a two dimensional array would have "rows" and "columns" A variant array is set up so you can store anything in the individual elements (intersect of row and column).

How do you declare an array variant?

Declare a fixed array Each numeric Variant element of the array uses 16 bytes. Each string Variant element uses 22 bytes. To write code that is as compact as possible, explicitly declare your arrays to be of a data type other than Variant. The following lines of code compare the size of several arrays.


1 Answers

The for is useless, as far as I can see. Better ReDim flat and generate flat2 as below

ReDim flat(UBound(lines))
flat2 = Join(flat,"|")

in fact, given that lines is coming in as ByVal you could probably

flat2 = Join(lines,"|")
like image 76
bugmagnet Avatar answered Sep 28 '22 05:09

bugmagnet