Save each row as txt, with some ects

Whaletacos

New Member
Joined
Aug 10, 2017
Messages
13
Hi everybody,

So I have been tinkering with getting excel to save each row as a txt file, without much luck.

The format is as follows,

Cells (1,1) contains the filename

The contents of the first .txt file then needs to be Cells(1,1;1,2)
The contents of the second .txt file needs to be Cells(2,1;2,2)
as so forth
Until it hits an empty cell.

The output path needs to be the current path of the workbook.


Any input will be much appriciated :)

- Whaletacos
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hello,
something like,,,
Code:
Option Explicit
Sub DataExport()
    Dim CellItem As Range
    Open ThisWorkbook.Path & "\RawTest.txt" For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
    For Each CellItem In Range("A8:M17", "A22:D207")' change range/cell to suit
        If Not CellItem.Locked And Not CellItem.Value = vbNullString Then
            Write [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , CellItem.Worksheet.Name, CellItem.Row, CellItem.Column, CellItem.Value
        End If
    Next
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
End Sub

Sub DataImport()
    Dim strWS As String
    Dim lngRow As Long
    Dim lngColumn As Long
    Dim CellValue
    Open ThisWorkbook.Path & "\RawTest.txt" For Input As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
    Do While Not EOF(1)
        Input [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , strWS, lngRow, lngColumn, CellValue
        With Worksheets(strWS)
            .Cells(lngRow, lngColumn).Value = CellValue
        End With
    Loop
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
End Sub
 
Upvote 0
tHMfajN
Hi Pike,

Thanks for the quick response!

Reading my thread again, I see it comes off rather unclear.

The data is in excel, and needs to be "Written" to a .txt
The field with "8000-01" is A1
8000-01GGGTTTAAACCC
8000-02CCCAAAGGGTT
8000-03TTTAAGGGCC
8000-04AAAGGGTTTCCC

<tbody>
</tbody>

I'm trying to get excel to write a .txt file for each of these lines.

The name of the file, should be "8000-01"
The content should be "8000-01,GGGTTTAAACCC"

File two should be "8000-02,CCCAAAGGGTT" and so on.

All files should be written to the same folder as the workbook is in.
Example
"Y:\Currentset\Seqs\8000"
tHMfajN
 
Upvote 0
Hello,
One way,,,
Code:
Option Explicit
Sub DataExport()
    Dim lngRow As Long
    For lngRow = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        Open ThisWorkbook.path & "\" & Cells(lngRow, 1).Value & ".txt" For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
        Write [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , Join(Application.Transpose(Application.Transpose(Range("A" & lngRow & ":B" & lngRow).Value)), ",")
        Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
    Next
End Sub
 
Upvote 0
Hi Pike!

Thank you so much, it worked wonders!

I adapted a few minor details, from write to print to avoid (") qoutation marks in the printed file.
And changed "Thisworkbook.path" to "activeworkbook.path" so I could throw it in the personal folder without it printing the file to XLSTART.
Thank you again!

The final code for good measure

Code:
Option ExplicitSub DataExport()
    Dim lngRow As Long
    For lngRow = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        Open ActiveWorkbook.Path & "\" & Cells(lngRow, 1).Value & ".txt" For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
        Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , Join(Application.Transpose(Application.Transpose(Range("A" & lngRow & ":B" & lngRow).Value)), ",")
        Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
    Next
'Credits to pike from mrexcel.com
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,388
Messages
6,124,659
Members
449,178
Latest member
Emilou

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