ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   save and add to CSV file (https://www.excelbanter.com/excel-programming/405726-save-add-csv-file.html)

Helmut

save and add to CSV file
 
I want to save data as CSV file (with specific name) and if CSV file doesn't
exist - create it and if CSV file does exist to 'add' values to bottom of
existing csv file rows.

Help!?

joel

save and add to CSV file
 
Sub AppendCSV()

Const MyPath = "C:\temp\"
Const WriteFileName = "textb.csv"

Const Delimiter = ","

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Set fswrite = CreateObject("Scripting.FileSystemObject")

'open files
WritePathName = MyPath + WriteFileName
If Dir(WritePathName) = "" Then
fswrite.CreateTextFile WritePathName
End If
Set fwrite = fswrite.GetFile(WritePathName)
Set ts = fwrite.OpenAsTextStream(ForAppending, TristateUseDefault)

LastRow = Cells(Rows.Count, "A").End(xlUp).Row

For RowCount = 1 To LastRow
LastCol = Cells(RowCount, Columns.Count).End(xlToLeft).Column
For ColCount = 1 To LastCol
If ColCount = 1 Then
OutPutLine = Cells(RowCount, ColCount)
Else
OutPutLine = OutPutLine & Delimiter & Cells(RowCount, ColCount)
End If
Next ColCount
ts.writeline OutPutLine
Next RowCount

ts.Close

End Sub



"Helmut" wrote:

I want to save data as CSV file (with specific name) and if CSV file doesn't
exist - create it and if CSV file does exist to 'add' values to bottom of
existing csv file rows.

Help!?



All times are GMT +1. The time now is 05:15 PM.

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