Insert Header Row to Text File

gofalc

New Member
Joined
Aug 11, 2014
Messages
3
I need to insert a Header row to existing text file (VBA Code).

Here is my code;


Sub header()
'the final string to print in the text file
Dim strData As String
'each line in the original text file
Dim strLine As String
Dim time_date As String


strData = ""
time_date = Format(Date, "yyyymmdd")
'open the original text file to read the lines
Open "C:\Temp\Test.txt" For Input As #1
'continue until the end of the file


While EOF(1) = False
'read the current line of text
Line Input #1, strLine
'add the current line to strData
strData = strData + strLine & vbCrLf
Wend

'add the new line
strData = "header row" & " " + time_date + " " + strData
Close #1
'reopen the file for output
Open "c:\Temp\Test.txt" For Output As #1
Print #1, strData
Close #1
End Sub


It is appending Header row to FIRST row data instead Inserting a new row to top.....
For Example:
header row 20140812 HELLO THIS IS MY TEST

I need;
header row 20140812
HELLO THIS IS MY TEST ...

Thanks for your help!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I m using this code and its working fine but it is adding one extra blank row at the end of document.
Any suggestions on how to fix it.


I need to insert a Header row to existing text file (VBA Code).

Here is my code;


Sub header()
'the final string to print in the text file
Dim strData As String
'each line in the original text file
Dim strLine As String
Dim time_date As String


strData = ""
time_date = Format(Date, "yyyymmdd")
'open the original text file to read the lines
Open "C:\Temp\Test.txt" For Input As #1
'continue until the end of the file


While EOF(1) = False
'read the current line of text
Line Input #1 , strLine
'add the current line to strData
strData = strData + strLine & vbCrLf
Wend

'add the new line
strData = "header row" & " " + time_date + " " + strData
Close #1
'reopen the file for output
Open "c:\Temp\Test.txt" For Output As #1
Print #1 , strData
Close #1
End Sub


It is appending Header row to FIRST row data instead Inserting a new row to top.....
For Example:
header row 20140812 HELLO THIS IS MY TEST

I need;
header row 20140812
HELLO THIS IS MY TEST ...

Thanks for your help!
 
Upvote 0
It is appending Header row to FIRST row data instead Inserting a new row to top.....
For Example:
header row 20140812 HELLO THIS IS MY TEST

I need;
header row 20140812
HELLO THIS IS MY TEST ...
See if changing this line of code...

strData = "header row" & " " + time_date + " " + strData

to this....

strData = "header row" & " " + time_date + vbLf + strData

does what you want.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,688
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top