Create a text file for every cell in column A

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I have a workbook with one column (column A) and 400 rows

I would like to create a ".txt" file for every cell.


For example...
If the text in cell A2 says "Orange and Apple", I would like a text file named "Orange and Apple.txt" to be created in the "C:\My Documents" folder.


Thanks much!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this:
Code:
Sub NewTXTfromColA()
    Dim Filename As String
    Dim TextFile As Integer
    Dim FilePath As String
    For Each cell In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        Filename = cell
        FilePath = "C:\Users\chris\Desktop\" & Filename & ".txt "
        TextFile = FreeFile
        Open FilePath For Output As TextFile
        Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=TextFile]#TextFile[/URL] , Filename
        Close TextFile
    Next
End Sub

Of course, add in some error handling to make sure the file name is valid and doesn't contain any forbidden characters.
 
Upvote 0
try this:

Rich (BB code):
Sub Creat_Txt_File()

mydir = "c:\my Documnets\"

For r = 2 To 400
Data = Cells(r, "A")
If Data <> "" Then
Open mydir & Data & ".txt" For Append As #1 
Print #1 , Data
Close #1 
End If
Next r

End Sub

hth,

Ross
 
Upvote 0

Forum statistics

Threads
1,214,893
Messages
6,122,121
Members
449,066
Latest member
Andyg666

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