Create txt file for each cell in a range

neeraj_chow

Board Regular
Joined
Aug 1, 2003
Messages
62
I am using excel 2003. I want a VBA code which when run would ask me range of cells as input to the function/proc and create seperate text files for values placed in each cell of the range. i.e. if the range has 10 cells, then there would be 10 files created with cell ref as filename eg filenames could be a1, a2, a3 etc

Require assistance on this one

Regards
Neeraj
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi

seems a strange request but here's some code that should do it. Change 'mypath" to the folder you want to save the files.


Code:
Public Sub makeCellFile()
    
Dim myRange As Range
Dim mycell As Range
Dim mypath As String
mypath = "C:\Test\"
    
Set myRange = Application.InputBox("Select Range", "Create Files in Range", Type:=8)
     
For Each mycell In myRange
        
    myfile = Replace(mycell.Address, "$", "") & ".txt"
    myfile = mypath & myfile
    FileNum = FreeFile
        
    Open myfile For Output As FileNum
    Print #FileNum, mycell
    Close FileNum
        
Next
    
MsgBox "Files created"

End Sub


atb
sumuwin
 
Upvote 0
Question...

How can I modify this vba to use the text contained in a cell as the .txt filename?

Public Sub makeCellFile()

Dim myRange As Range
Dim mycell As Range
Dim mypath As String
mypath = "C:\Misc"

Set myRange = Application.InputBox("Select Range", "Create Files in Range", Type:=8)

For Each mycell In myRange

myfile = Replace(mycell.Address, "$", "") & ".txt"
myfile = mypath & myfile
FileNum = FreeFile

Open myfile For Output As FileNum
Print #FileNum, mycell
Close FileNum

Next

MsgBox "Files created"

End Sub
 
Upvote 0
Change this:

Code:
myfile = Replace(mycell.Address, "$", "") & ".txt"


To this:

Code:
myfile = mycell.value & ".txt"
 
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,089
Members
448,548
Latest member
harryls

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