Hello,
I currently have an excel file with 1000+ columns and would like to export each individual one into a .txt file. The first row of each column has a different name, which I would also like to be the name of the .txt file created. I've tried using macros that I found browsing this site, but have been unsuccessful thus far. I attached a excel file I'm working with (smaller version) for a clearer picture.
Thanks for the help/insight!!
Hi,
I found and edit this script below, see if this does what you need. Leith Ross was the person who made the orginal script. But this should get you going in the right direction with some of my edits. Post back any issue's you may have.
.Sub CreateTextFile() Const ForAppending As Long = 8 Const DefaultFormat As Long = -2 Const UnicodeFormat As Long = -1 Const AsciiFormat As Long = 0 Dim C As Long, R As Long Dim FileName As String Dim Filepath As String Dim fso As Object Dim LastCol As Long Dim LastRow As Long Dim Rng As Range Dim StartCol As Long Dim StartRow As Long Dim TxtData As String Dim TxtFile As Object Dim MyName As String Dim MyRow As Long Dim MyCol As Long StartRow = 1 StartCol = 1 MyRow = 1 MyCol = 1 LastCol = Cells(StartRow, Columns.Count).End(xlToLeft).Column LastRow = Cells(Rows.Count, StartCol).End(xlUp).Row For C = StartCol To LastCol MyName = Cells(MyRow, MyCol).Value Set fso = CreateObject("Scripting.FileSystemObject") Set TxtFile = fso.OpenTextFile("c:\Test\" & MyName & ".txt", ForAppending, DefaultFormat) For R = StartRow + 1 To LastRow Set Rng = Range(Cells(StartRow, C), Cells(LastRow, C)) If WorksheetFunction.CountIf(Rng, "<>*") <> 0 Then TxtData = TxtData & Cells(R, C).Value & vbTab End If Next R TxtFile.WriteLine (TxtData) TxtData = "" MyCol = MyCol + 1 TxtFile.Close Set fso = Nothing Set TxtFile = Nothing Next C End Sub
Thank You, Mike
Some Helpful Hints:
1. New members please read & follow the Forum Rules
2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
3. If you are pleased with a solution mark your post SOLVED.
4. Thank those who have help you by clicking the scales at the top right of the post.
Here...
Thanks for the reply realniceguy5000. Everything looks good except the resulting data in the text files is displayed a little different then I would prefer. Is there a way to arrange the data vertically like this:
0
0
2
0
19
0
0
0
Instead of the current 4 row layout as seen below?
0 5 0 0 0
0 0 0 0 0
3 0 0 2 0
0 2 0 0 4
Everything else looks great. Thanks again for all the help.
Spoke too soon, I just noticed something else. The first 3 columns didn't get processed correctly. It outputs one file with the name of the first 3 columns combined. This is also the same for the text inside of the file.
Hello vund0,
This macro will create a file for column on the worksheet using the cells in row 1 as the file name. You will need to change the file path to where your files will be stored.
Sub ExportDataToFiles() Dim Col As Range Dim Data As Variant Dim FileName As String Dim FilePath As String Dim FSO As Object Dim Item As Variant Dim TextFile As Object Dim Wks As Worksheet FilePath = "C:\Test Folder\" Set Wks = Worksheets("pplacer_resampled1021") Set Rng = Wks.UsedRange Set FSO = CreateObject("Scripting.FileSystemObject") For Each Col In Rng.Columns ReDim Data(1 To Col.Rows.Count, 1 To 1) FileName = FilePath & Col.Cells(1, 1) Data = Col.Offset(1, 0).Resize(Col.Rows.Count - 1, 1).Value Set TextFile = FSO.OpenTextFile(FileName, 2, True, False) For Each Item In Data TextFile.WriteLine Item Next Item TextFile.Close Next Col End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Thanks Leith Ross. You are a gentleman and a scholar.
Hello vund0,
You're welcome. Glad to help.
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
or
If you go for speed: no need to load an extra library, nor to produce a new instance of an object.Sub snb() sq = Sheets(1).Cells(1).CurrentRegion For j = 1 To ubound(sq,2) Open "E:\OF\" & sq(1, j) & ".txt" For Output As #1 Print #1, Join(Application.Transpose(Application.Index(sq, , j)), vbCrLf) Close Next End Sub
Open, print & Close are VBA-commands, that are resident in memory.
The code has only 1 reading instruction.
The writing instructions are also diminished to a minimum (1 per column).
Last edited by snb; 06-02-2011 at 05:33 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks