VBA to create multiple .txt files?

cbaird2

New Member
Joined
Feb 16, 2011
Messages
11
Hello,
I have an Excel 2010 sheet with 2 columns, 1 column with a list of dates that I want to use as part of a file name, and another column with formatted information that I want to put into separate .txt files. I have no idea where to begin to write a macro to do this, but I know copy/paste manually would take forever and would no doubt end up in some screw ups.

In this file, it is D2:D493 for the file name, and E2:E493 for the text in the files. Each file will have 2 lines of text (so D2 and D3 are the same date, and so on down the column). I'd like the macro to insert "PRICE" infront of the date in the file name.

Does anyone have anything like this available? Thanks in advance for any help - it would be a giant time saver for me!

thanks again!!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
If I've understood you correctly this should do what you want. Just change the output folder string and date format (I've used yyyymmdd) in the code.
Code:
Sub Create_Files()

    Dim row As Long
    Dim fileName As String, folder As String
    
    folder = "C:\temp\Excel\"
    
    For row = 2 To 493 Step 2
        fileName = "PRICE" & Format(Cells(row, "D").Value, "yyyymmdd") & ".txt"
        Open folder & fileName For Output As #1
        Print #1, Cells(row, "E").Value
        Print #1, Cells(row + 1, "E").Value
        Close #1
    Next
    
End Sub
 
Upvote 0
John w, this worked marvelously. This saved me a boatload of time. I already lost one day's work trying to create a shortcut on this project, and you just saved me at least 3 hours minimum plus. Thank you SO much!!
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,253
Members
452,900
Latest member
LisaGo

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