How do I convert each column in Excel into its own .txt file?

aespinosa

New Member
Joined
Jun 4, 2015
Messages
2
Hello;

I have a spreadsheet called ManyTXT. Each column provides data (in seconds) for a single person. I have a total of 46 people, so the spreadsheet has 46 columns. I would like to convert each column into its own separate .txt file with the name of the person for whom the column (or .txt file) corresponds to. The names of the individuals are on the first row as follows:

Person1 Person2 Person3 ... Person46
seconds seconds seconds seconds
seconds seconds seconds seconds
seconds seconds seconds seconds
seconds seconds seconds seconds
seconds ...

Can someone please help me get the vba code? I have successfully ran a code that saves each row. I need each column and for the life of me can't seem to alter the code. Any help will be eternally appreciated.
thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Right click the sheet tab and click "View Code", then paste this code in the window that opens:
Code:
Sub ColumnsToTXT()Dim thisCol, newSht, c, r
Dim ws As Worksheet
Dim pathname As String
Dim DataRange As Range


Set DataRange = Cells(1, 1).Resize(UsedRange.Rows.Count, UsedRange.Columns.Count)


Application.DisplayAlerts = False


For c = 1 To UsedRange.Columns.Count
' Copy the column to a new temp sheet
    r = Cells(100000, c).End(xlUp).Row
    thisCol = Cells(1, c).Resize(r).Value
    newSht = Cells(1, c).Value
'Chech that the sheet doesn't already exist and delete if it does
    On Error Resume Next
    Set ws = Sheets(newSht)
    On Error GoTo 0
    If Not ws Is Nothing Then ws.Delete
'Add the new sheet
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = newSht
    Sheets(newSht).Cells(1, 1).Resize(r).Value = thisCol
' Save the sheet as .txt file
    pathname = "c:\" & newSht & ".txt" 'Change the directory as required
    Sheets(newSht).Copy
'Close the file
    ActiveWorkbook.SaveAs Filename:=newSht, _
        FileFormat:=xlTextWindows, CreateBackup:=False, ConflictResolution:=xlLocalSessionChanges
    ActiveWorkbook.Close
'Delete the redundant sheet
    Worksheets(newSht).Delete
Next c


Application.DisplayAlerts = True


End Sub

Dan
 
Upvote 0
Hi..

Change sheet name to suit. Text files will be saved in same Folder as your workbook.

Code:
[FONT=Courier New][COLOR=#0000FF]Sub[/COLOR][COLOR=#000000] CreateTextFiles[/COLOR][COLOR=#000000]()[/COLOR]
[/FONT]Dim x, i As Long
    x = Sheets("Sheet1").[A1].CurrentRegion
    For i = LBound(x, 2) To UBound(x, 2)
     With CreateObject("Scripting.FileSystemObject").CreateTextFile(ThisWorkbook.Path & "\" & x(1, i) & ".txt", True)
            .writeline Join(Application.Transpose(Application.Index(x, 0, i)), vbCrLf)
    End With
    Next i
    MsgBox "Text Files Saved to: " & ThisWorkbook.Path
[FONT=Courier New][COLOR=#0000FF]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000FF]Sub[/COLOR]
[/FONT]
 
Upvote 0
Thanks a million to all who helped. I got it!!!!

Adriana


Hi..

Change sheet name to suit. Text files will be saved in same Folder as your workbook.

Code:
[FONT=Courier New][COLOR=#0000FF]Sub[/COLOR][COLOR=#000000] CreateTextFiles[/COLOR][COLOR=#000000]()[/COLOR]
[/FONT]Dim x, i As Long
    x = Sheets("Sheet1").[A1].CurrentRegion
    For i = LBound(x, 2) To UBound(x, 2)
     With CreateObject("Scripting.FileSystemObject").CreateTextFile(ThisWorkbook.Path & "\" & x(1, i) & ".txt", True)
            .writeline Join(Application.Transpose(Application.Index(x, 0, i)), vbCrLf)
    End With
    Next i
    MsgBox "Text Files Saved to: " & ThisWorkbook.Path
[FONT=Courier New][COLOR=#0000FF]End[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000FF]Sub[/COLOR]
[/FONT]
 
Upvote 0

Forum statistics

Threads
1,203,396
Messages
6,055,161
Members
444,766
Latest member
bryandaniel5

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