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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

sumuwin

New Member
Joined
Sep 8, 2009
Messages
38
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
 

neeraj_chow

Board Regular
Joined
Aug 1, 2003
Messages
62
The request was indeed strange, but thanks dude, that was exactly what I was looking for ;)
 

kev6264

New Member
Joined
Jun 16, 2006
Messages
44
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
 

TheLazyBone

New Member
Joined
Oct 28, 2010
Messages
7
Change this:

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


To this:

Code:
myfile = mycell.value & ".txt"
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,920
Messages
5,834,375
Members
430,282
Latest member
MeredithD26

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
Top