Save Individual Cells as Separate Text Files

nmoloney1968

New Member
Joined
Feb 18, 2007
Messages
20
I have a large dataset that will be read by a statistics package. The data has to be a txt file for each sample (the sample is a text string that is being analyzed). So I have a Filename column (A) and a Sample column (B), which have 1400+ rows (i.e. 2 x 1400 cells). How can I program a VBA macro to take the contents of Column B, row n, put it into a text file, then save that text file with the name in Column A, row n, and loop on down all the rows so I end up with 1400 or so files?

The text in the Sample can be quite long (but not more than say 2000-ish characters).

Neil.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I solved it myself. Firstly I had to use the following formula to overcome the 1024 character limit: =MID($B2,1025,1024) and incrementing the '1025' by 1024 across a few columns until I had captured all of the text. I found out the longest text using a MAX() and created enough columns to suit. Then I used the following code to create the text files (modified from Chip Pearson at http://www.cpearson.com/excel/ImpText.aspx):

Sub ExportToTextFile()

Dim FNum As Integer
Dim CellValue As String
Dim FName As String
Dim Counter As Integer

Application.ScreenUpdating = False
On Error GoTo EndMacro:

For Counter = 2 To 1487
Worksheets("Sheet1").Cells(Counter, 2).Select
FName = "C:\Documents and Settings\My Documents\MY Stuff\Text Files\" & Cells(Counter, 1).Text & ".txt"
FNum = FreeFile

CellValue = Cells(Counter, 2).Text & Cells(Counter, 3).Text & Cells(Counter, 4).Text & Cells(Counter, 5).Text

Open FName For Output Access Write As #FNum

Print #FNum, CellValue
Close #FNum

Next Counter

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #FNum

End Sub


I hope this helps someone in the future!

Neil.
 
Upvote 0
Hi Neil

Always good when you solve something yourself - you'll certainly remember it better and thanks for taking the time to paste your solution to the board. One question though - what is this 1024 character limit you are talking about? Cells can hold (in all versions of Excel that I have ever dealt with) 32767 characters. String variables can hold 2 billion +, so I am surprised you had to chop up the contents of your cells.
 
Upvote 0
Good question - I have no answer to this, but it may just be that when you pull the string the way I did, only the first 1024 characters are recognized? Just guessing...

Happy New Year!
 
Upvote 0
Sorry, yes - you're using the Text property no the the Value property of the range object. This will limit you to 1024 characters.
 
Upvote 0
That's interesting Richard...Presumably the Text property is "as displayed in the cell" and the cell is limited in what it can display...
 
Upvote 0
Yep - something which I have only just managed to get into my head is that the Text property is read only too. Seems ridiculous I know, but it's one of those things that just doesn't "stick" in my brain so time after time I try and write to a range using Text - D'Oh!
 
Upvote 0

Forum statistics

Threads
1,214,661
Messages
6,120,796
Members
448,994
Latest member
rohitsomani

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