Modify code to export Cell contents to multiple Text Files

tkeiffer

New Member
Joined
Aug 5, 2005
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have 200 cells in column D with text in them. I want to use a macro to generate 200 separate text files containing each individual cell contents. If possible, I'd like to name of each text file to be based on the cell adjacent in Column C. Using the macro below, I can generate a text file from what ever cell I have highlighted. Since I'm not a good macro writer, I'm not sure how to modify the code so it does what I need. If someone could help me out I would be so grateful!!!

Sub Export()
Dim r As Range, c As Range
Dim sTemp As String

Open "c:\MyOutput.txt" For Output As #1
For Each r In Selection.Rows
sTemp = ""
For Each c In r.Cells
sTemp = sTemp & c.Text & Chr(9)
Next c

'Get rid of trailing tabs
While Right(sTemp, 1) = Chr(9)
sTemp = Left(sTemp, Len(sTemp) - 1)
Wend
Print #1, sTemp
Next r
Close #1


End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I got my answer from another site. Here is the code in case anyone is interested:

Sub Export()
Dim cell As Range
Dim sTemp As String

VBA.FileSystem.MkDir "c:\TextFileHolder\"
For Each cell In [D1:D200]
Open "c:\TextFileHolder\" & cell.Offset(0, -1).Value & ".txt" For Output As #1
sTemp = cell.Value

'Get rid of trailing tabs
While Right(sTemp, 1) = Chr(9)
sTemp = Left(sTemp, Len(sTemp) - 1)
Wend

Print #1, sTemp

Close #1
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,815
Messages
6,121,715
Members
449,049
Latest member
THMarana

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