Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm looking to create a .txt file for each value in column A containing the corresponding values in columns B and C

share|improve this question

2 Answers 2

up vote 1 down vote accepted

The more I looked at this the more I found it to be a useful little macro. In order to keep it from processing blank rows (and locking up my Excel) I rewrote the code to only create a file while there is data available. Also, the use of print rather than write creates text without the quotations. Here is what I used to accomplish the same thing.

Sub CreateFile()
Do While Not IsEmpty(ActiveCell.Offset(0, 1))
    MyFile = ActiveCell.Value & ".txt"
    'set and open file for output
    fnum = FreeFile()
    Open MyFile For Output As fnum
    'use Print when you want the string without quotation marks
    Print #fnum, ActiveCell.Offset(0, 1) & " " & ActiveCell.Offset(0, 2)
Close #fnum
ActiveCell.Offset(1, 0).Select
Loop
End Sub

Feel free to use and modify as you wish. Thanks for the great idea.

share|improve this answer

This macro will take each value in column A, produce a .txt document in the name of the value and insert the corresponding information from columns B and C

Sub CreateTxt()
'
For Each ce In Range("A1:A" & Cells(Rows.Count, 1).End(xlDown).Row)
    Open ce & ".txt" For Output As #1
    Write #1, ce.Offset(0, 1) & " " & ce.Offset(0, 2)
    Close #1
Next ce
End Sub
share|improve this answer
    
Great idea. This code created the text files for the data I have in two rows, but then causes Excel to stop responding. The line For Each ce In Range("A1:A" & Cells(Rows.Count, 1).End(xlDown).Row) returns over a millions rows even though only two are populated. –  CharlieRB Feb 7 '12 at 21:33
    
That hasn't happened to me, but I think I select it and use a keyboard shortcut to run the macro. Ideally, I should put in my range, but recently it's been varying too much –  Raystafarian Feb 7 '12 at 22:22
    
Additionally, you could have it start at the bottom and xlUp to the top to avoid there being blank sheets. I'm wide open to a more elegant solution. –  Raystafarian Feb 8 '12 at 13:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.