Click to See Complete Forum and Search --> : Variant Array to single line Text file


Shan77
May 7th, 2001, 08:02 PM
I have an array with a variable number of elements and i'm trying to read it into a text file with all the elements on the same line ! ...

I'm able to put the array elements in a text file one line at a time using the following:

Open App.Path & "\test.txt" For Output As #1

Dim v As Variant
For Each v In input1
Print #1, v
Next

Close #1

Where code above this has already read in the values for the Input1() array from an input grid.

Does someone know how to write it all on the same line ??

Thanks,

Shannon.

cksiow
May 7th, 2001, 10:36 PM
what you need to do is to join the array into a single string and write to the text file at once like this

Dim v As Variant
dim s as string
s = ""
For Each v In input1
s = s + cstr(v)
Next

Print #1, s

shree
May 8th, 2001, 09:19 AM
You can use the Join() function. It returns a string created by joining a number of substrings contained in an array. And you can optionally specify a delimiter too.

dfwade
May 8th, 2001, 09:36 AM
Dim v() As Variant
ReDim Preserve v(4)
v(0) = "This"
v(1) = "is"
v(2) = "a"
v(3) = "Test"
Dim str As String
str = Join(v, " ")
Print str