ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Trying to append to file using an array (https://www.excelbanter.com/excel-programming/349555-trying-append-file-using-array.html)

susan hayes

Trying to append to file using an array
 
Hi
Im trying to append the info in an array which has numbers and letters. I cannot get the array to write to it.
This does work because Ive tried the appending "hello World" to it. It also appends the ","
Any help is appreciated
Thanks
And Thanks again for the many helps this group has provided to me
Mike

Dim myarray(19) as Variant

Dim fs, f, ts, s, chk
Set fs = CreateObject("Scripting.FileSystemObject")
chk = fs.FileExists("c:\data.txt")

If chk = True Then ' check if file exists
Set f = fs.GetFile("c:\data.txt")
Set ts = f.OpenAsTextStream(8, 0)
ts.write "Hello World"
For i = 1 To 19
ts.write myarray(i) & ","
Next
ts.write vbCrLf
ts.Close
Else 'if not create file
fs.CreateTextFile "c:\data.txt" 'creates file
Set f = fs.GetFile("c:\data.txt")
Set ts = f.OpenAsTextStream(8, 0)
ts.write "Hello World"
For i = 1 To 19
ts.write myarray(i) & ","
Next
ts.write vbCrLf
ts.Close
End If

NickHK

Trying to append to file using an array
 
Susan,
Any good reason why you are using the FSO in VBA ?

If not, then check out VBA's "Open" statement. It will be more simple
because:
"If the file specified by pathname doesn't exist, it is created when a file
is opened for Append, Binary, Output, or Random modes."

NickHK

"Susan Hayes" wrote in message
...
Hi
Im trying to append the info in an array which has numbers and letters. I

cannot get the array to write to it.
This does work because Ive tried the appending "hello World" to it. It

also appends the ","
Any help is appreciated
Thanks
And Thanks again for the many helps this group has provided to me
Mike

Dim myarray(19) as Variant

Dim fs, f, ts, s, chk
Set fs = CreateObject("Scripting.FileSystemObject")
chk = fs.FileExists("c:\data.txt")

If chk = True Then ' check if file exists
Set f = fs.GetFile("c:\data.txt")
Set ts = f.OpenAsTextStream(8, 0)
ts.write "Hello World"
For i = 1 To 19
ts.write myarray(i) & ","
Next
ts.write vbCrLf
ts.Close
Else 'if not create file
fs.CreateTextFile "c:\data.txt" 'creates file
Set f = fs.GetFile("c:\data.txt")
Set ts = f.OpenAsTextStream(8, 0)
ts.write "Hello World"
For i = 1 To 19
ts.write myarray(i) & ","
Next
ts.write vbCrLf
ts.Close
End If





All times are GMT +1. The time now is 04:38 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com