Export data one line at a time to txt file

jjokol

Board Regular
Joined
Dec 21, 2008
Messages
213
I have a few thousand rows of text data and I want each line to be exported to a single text file. How can I achieve this?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Code:
Sub ExportRows()

    ' Set reference to library: Tools -> Reference -> Microsoft Scripting Runtime
    
    Dim i As Long, sPath As String
    Dim fso As New FileSystemObject, txtFile As TextStream
    
    sPath = ThisWorkbook.Path & "\"
    
    ' Adjust start cell. Mine is A1
    For i = 1 To Range("A").End(xlDown).Row
        Set txtFile = fso.CreateTextFile(sPath & "text" & i & ".txt")
        txtFile.WriteLine Cells(i, 1)
        txtFile.Close
    Next
    
End Sub
 
Upvote 0
Thanks but I keep getting this error:

'Runtime error '1004'
Method 'Range' of object '_Global failed

It highlights this line of code:
For i = 1 To Range("A").End(xlDown).Row
 
Upvote 0
How do you want the data formatted?

Do you went it delimited anyway?

Perhaps comma separated.
 
Upvote 0
Is it entire rows or just certain columns?

PS Try Range("A"&Rows.Count).End(xlUp).Row in the code already posted.
 
Upvote 0
Where is your data located? My code assumes that you data is in column A. It takes all cell's content and saves to text file.
 
Upvote 0
Code:
Sub ExportRows()

    ' Set reference to library: Tools -> Reference -> Microsoft Scripting Runtime
    
    Dim i As Long, sPath As String
    Dim fso As New FileSystemObject, txtFile As TextStream
    
    
    sPath = ThisWorkbook.Path & "\"
    
    ' Adjust start cell. Mine is A1
    For i = 1 To Range("A").End(xlDown).Row
        Set txtFile = fso.CreateTextFile(sPath & "text" & i & ".txt")
        txtFile.WriteLine Cells(i, "A") & "; " & _
                          Cells(i, "B") & "; " & _
                          Cells(i, "C") & "; " & _
                          Cells(i, "D")
        txtFile.Close
    Next
    
End Sub
 
Upvote 0
Sektor

I think there might be a typo, shouldn't it be Range("A1")?
 
Upvote 0

Forum statistics

Threads
1,224,562
Messages
6,179,526
Members
452,923
Latest member
JackiG

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