ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Insert line in first row of CSV text file with VBA (https://www.excelbanter.com/excel-programming/414143-insert-line-first-row-csv-text-file-vba.html)

JA[_4_]

Insert line in first row of CSV text file with VBA
 
I have a CSV text file and would like to insert a row at the top using the
VBA Write statement from Excel. I am able to append, but can not add to the
first line.



RB Smissaert

Insert line in first row of CSV text file with VBA
 
Try this code:

Sub InsertLineAtBeginningTexFile(strFile As String, strLine As String)

Dim hFile As Long
Dim FileContents As String
Dim NewString As String

hFile = FreeFile
Open strFile For Binary As #hFile
FileContents = Space(FileLen(strFile))
Get #hFile, , FileContents
Close #hFile

NewString = strLine & vbCrLf
FileContents = NewString & FileContents

Open strFile For Binary As #hFile
Put #hFile, , FileContents
Close #hFile

End Sub


RBS


"JA" wrote in message
...
I have a CSV text file and would like to insert a row at the top using the
VBA Write statement from Excel. I am able to append, but can not add to
the first line.




All times are GMT +1. The time now is 04:51 PM.

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