All,
I have a 200-row text file laid out as follows:
| Column 1 | Column 2 |
| target1.txt | my string 1|
| target2.txt | my string 2|
| target3.txt | my string 3|
| target4.txt | my string 4|
etc...
| target200.txt | my string 200|
Anyone know a way to set up a macro in Excel that will save each | my
string n| as a text file named according to the value in Column 1?
And for bonus points, In an ideal world, the text file created will
also have one other line in it before the |string|, which will remain
constant across all 200 files. If I have to insert that constant text
into the Excel file and write two rows from Excel, I'm prepared to do
that if someone can suggest a way to write one!.
Thanks.
Patrick
Dave Peterson - 18 Dec 2006 17:24 GMT
Make sure that the folder you want to put the files in already exists:
Option Explicit
Sub CreateFiles()
Dim wks As Worksheet
Dim iRow As Long
Dim myHeader As String
Dim myFolderName As String
Dim FileNum As Long
myFolderName = "C:\temp\"
Set wks = Worksheets("sheet1")
myHeader = "Some Header Information here"
FileNum = FreeFile
With wks
For iRow = 1 To .Cells(.Rows.Count, "B").End(xlUp).Row
Close #FileNum
Open myFolderName & .Cells(iRow, "A").Value For Output As FileNum
Print #FileNum, myHeader
Print #FileNum, .Cells(iRow, "B").Value
Next iRow
End With
Close #FileNum
MsgBox "Done."
End Sub
> All,
>
[quoted text clipped - 20 lines]
>
> Patrick

Signature
Dave Peterson
parkins - 19 Dec 2006 14:52 GMT
Dave -
Works like a charm - my eyes and fingers thank you.
OK if I post your code on one of the Adobe forums - it's part of an
issue I was struggling with to update PDFs.
Patrick
Dave Peterson - 19 Dec 2006 15:15 GMT
Glad it worked for you.
When I want to refer to an older post, I'll find it on google and then post a
link.
http://groups.google.co.uk/group/microsoft.public.excel/browse_frm/thread/9b1257
bf55455c0b/84288c512c3ea113?lnk=st&q=&rnum=1#84288c512c3ea113
or
http://snipurl.com/15bh9
But you can post it directly if you want.
> Dave -
>
[quoted text clipped - 4 lines]
>
> Patrick

Signature
Dave Peterson