Need to repeat the first row in every csv file created

anuradhagrewal

Board Regular
Joined
Dec 3, 2020
Messages
87
Office Version
  1. 2010
Platform
  1. Windows
Hi
My file in question is BSE.csv
I am looking at repeating the first row mainly containing in all the txt files
<ticker>,<date>,<open>,<high>,<low>,<close>,<volume>,<o/i>

I have this code

VBA Code:
Sub Create_Txt_File()

On Error Resume Next
myPath = "c:\test\"
Kill myPath & "*.csv" 'this will delete all that file type within that folder
On Error GoTo 0

Data = ""
For r = 2 To Cells(Rows.Count, "B").End(xlUp).Row
    lcol = Cells(r, Columns.Count).End(xlToLeft).Column
 
    For c = 1 To lcol
        delim = ""
        If c < lcol Then delim = ","
        Data = Data & Cells(r, c) & delim
    Next c
 
     
    Open myPath & Cells(r, "B") & ".csv" For Append As #1
    Print #1, Data
    Close #1
    Data = ""
Next r
 

End Sub

What I am looking is that
1)The first row say A1-H1 be repeated in every individual files created.
2)The csv files so created be automatically be converted into *.txt files

Please help

Regards

Anu
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
add header row to all files, also changed them fro csv to txt

VBA Code:
Sub Create_Txt_File()

On Error Resume Next
myPath = "c:\test\"
Kill myPath & "*.txt" 'this will delete all that file type within that folder
On Error GoTo 0

Header = ""
For c = 1 To 7
    Header = Header & Cells(1, c) & ","
Next c
    Header = Header & [H1] & vbLf


Data = ""
For r = 2 To Cells(Rows.Count, "B").End(xlUp).Row
    lcol = Cells(r, Columns.Count).End(xlToLeft).Column
 
    For c = 1 To lcol
        delim = ""
        If c < lcol Then delim = ","
        Data = Data & Cells(r, c) & delim
    Next c
    

     If Dir(myPath & Cells(r, "B") & ".txt") = "" Then Data = Header & Data 'file does not exist need to add header row.
     
     
    Open myPath & Cells(r, "B") & ".txt" For Append As #1
    
    Print #1, Data
    Close #1
    Data = ""
Next r
 

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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