Export rows to .txt

jessiehayden

New Member
Joined
Mar 8, 2024
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Hello,

Using Microsoft Excel Pro 2019 on Windows 10.

I have a sheet with two columns: code and line. I need to export this sheet into .txt files, as follows:
- all rows that have the same value in the 'code' column need to be included in the same .txt file, in the order that they appear in the sheet
- each .txt file has to be named after its corresponding unique 'code'
- each 'line' value needs to be its own paragraph in the .txt file

This is what my Excel sheet looks like:
excel.jpg



This is how I need the files to turn out like in the output folder:
files.jpg



And this is an example of what the final .txt file should look like:
txt.jpg


Is there a straightforward way to do this?

Thanks in advance for your time and help!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
How about this?

VBA Code:
Sub toText()
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")
Dim FSO As Object:      Set FSO = CreateObject("Scripting.FileSystemObject")
Dim AR() As Variant:    AR = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row).Value2
Dim Path As String:     Path = "C:\YourPathHere\"

For i = 1 To UBound(AR)
    SD(AR(i, 1)) = SD(AR(i, 1)) & AR(i, 2) & vbLf
Next i

For Each Item In SD
    With FSO.CreateTextFile(Path & Item & ".txt")
        .WriteLine SD(Item)
        .Close
    End With
Next Item

End Sub
 
Upvote 1
Solution
How about this?

VBA Code:
Sub toText()
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")
Dim FSO As Object:      Set FSO = CreateObject("Scripting.FileSystemObject")
Dim AR() As Variant:    AR = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row).Value2
Dim Path As String:     Path = "C:\YourPathHere\"

For i = 1 To UBound(AR)
    SD(AR(i, 1)) = SD(AR(i, 1)) & AR(i, 2) & vbLf
Next i

For Each Item In SD
    With FSO.CreateTextFile(Path & Item & ".txt")
        .WriteLine SD(Item)
        .Close
    End With
Next Item

End Sub
Thank you so much, that worked perfectly!
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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