Breaking up CSV files based on date into individual txt files

anuradhagrewal

Board Regular
Joined
Dec 3, 2020
Messages
85
Office Version
  1. 2010
Platform
  1. Windows
Hi Guys
I have this BSE.csv file that I want to use to update my data.
What I request the experts is to help me with creating individual txt files based on dates that are in column B.
The separator for creating a new txt file is in B column containing the date in (yyyymmdd) format
For eg :
There are approx 4434 entries for date 20240212(yyyymmdd).
Now when the date changes to 20240213 I request that a new txt file be created with the date name for eg : 20240213.txt only for all entries who are for the date 20240213 and so on.
Thus in the end I am looking for 5 txt files mainly from 20240212.txt, 20240213.txt, 20240214.txt,20240215.txt,20240216.txt
Similarly if there are 10,15, or 20 unique dates correspondingly new date files be created for all the given dates

Please help

Best Wishes

Anuradha
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
try this on a copy of your file. be WARNED - this will DELETE all existing csv files in you path!

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

hth,
Ross
 
Upvote 0
try this on a copy of your file. be WARNED - this will DELETE all existing csv files in you path!

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

hth,
Ross
Hi
Thank you so much. It does make individual txt files based on date. However I need to convert the csv file to xlsx file and copy the code in this excel file then run it.(Can this be fixed as I will need to covert the csv to xlsx)
There is just one help I need from you.
I request you to kindly please modify the code that Row 1 (A1-H1) appears in each text file with a comma separator in the first row of every txt file.
Something like this:
<ticker>,<date>,<open>,<high>,<low>,<close>,<volume>,<o/i>
Thank you for everything

Cheers

Anu


Rest all is fine.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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