Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
BigIan
 
Posts: n/a
Default Write cell info to external file

Is it possible to make an external file from the contents of a cell? For
example if I have "Text acbd" as the contents of cell A1, can I write it to a
new file called Text1.txt?
If so, can the file name Text1.txt be in another cell, B1 for example?
So, I've got A1 = "Text abcd", B1 = Text1.txt, is there a command or VB
routine that I can use to make a file called Txt1.txt with the contents "Text
abcd"?

Thanks,
Ian
  #2   Report Post  
Duke Carey
 
Posts: n/a
Default Write cell info to external file

The simple answer to your question is that you need to write VBA code to do
what you describe. You'll want to consider things like identifying the
cell(s) you want to have involved in the process, whether the cell with the
filename will also provide the path, etc.




"BigIan" wrote:

Is it possible to make an external file from the contents of a cell? For
example if I have "Text acbd" as the contents of cell A1, can I write it to a
new file called Text1.txt?
If so, can the file name Text1.txt be in another cell, B1 for example?
So, I've got A1 = "Text abcd", B1 = Text1.txt, is there a command or VB
routine that I can use to make a file called Txt1.txt with the contents "Text
abcd"?

Thanks,
Ian

  #3   Report Post  
Stefi
 
Posts: n/a
Default Write cell info to external file

Substitute "pathname" with your real pathname!

Sub txtsave()
ActiveWorkbook.ActiveSheet.SaveAs Filename:= _
"pathname" & Range("B1"), FileFormat:=xlText, _
CreateBackup:=False
End Sub


Regards,
Stefi

€žBigIan€ť ezt Ă*rta:

Is it possible to make an external file from the contents of a cell? For
example if I have "Text acbd" as the contents of cell A1, can I write it to a
new file called Text1.txt?
If so, can the file name Text1.txt be in another cell, B1 for example?
So, I've got A1 = "Text abcd", B1 = Text1.txt, is there a command or VB
routine that I can use to make a file called Txt1.txt with the contents "Text
abcd"?

Thanks,
Ian

  #4   Report Post  
BigIan
 
Posts: n/a
Default Write cell info to external file

Thanks for your help Stefi, I can't get it to work though. I get a 400 error
when I run it.
I'm not very well up in VB so this is just a guess, but does your code just
define the filename? I see pathname + filename from cell B1 but I don't see
where the cell content of A1 goes.
What I've done so far is to copy your code exactly on to a VB code box and
replace "pathname" with "c:\" (I left the quotation marks in) so it looks
like this:

Sub txtsave()
ActiveWorkbook.ActiveSheet.SaveAs Filename:= _
"c:\" & Range("B1"), FileFormat:=xlText, _
CreateBackup:=False
End Sub

Then I ran it as a macro. Do I need to change or add something else?

Thanks,
Ian

"Stefi" wrote:

Substitute "pathname" with your real pathname!

Sub txtsave()
ActiveWorkbook.ActiveSheet.SaveAs Filename:= _
"pathname" & Range("B1"), FileFormat:=xlText, _
CreateBackup:=False
End Sub


Regards,
Stefi

€žBigIan€ť ezt Ă*rta:

Is it possible to make an external file from the contents of a cell? For
example if I have "Text acbd" as the contents of cell A1, can I write it to a
new file called Text1.txt?
If so, can the file name Text1.txt be in another cell, B1 for example?
So, I've got A1 = "Text abcd", B1 = Text1.txt, is there a command or VB
routine that I can use to make a file called Txt1.txt with the contents "Text
abcd"?

Thanks,
Ian

  #5   Report Post  
Stefi
 
Posts: n/a
Default Write cell info to external file

What version do you use? I tested it in XL2000 and it worked!
Nonetheless try this:

Sub txtsave()
ActiveWorkbook.ActiveSheet.SaveAs Filename:= _
"c:\" & Range("B1"), FileFormat:=xlTextWindows, _
CreateBackup:=False
End Sub

Regards,
Stefi

€žBigIan€ť ezt Ă*rta:

Thanks for your help Stefi, I can't get it to work though. I get a 400 error
when I run it.
I'm not very well up in VB so this is just a guess, but does your code just
define the filename? I see pathname + filename from cell B1 but I don't see
where the cell content of A1 goes.
What I've done so far is to copy your code exactly on to a VB code box and
replace "pathname" with "c:\" (I left the quotation marks in) so it looks
like this:

Sub txtsave()
ActiveWorkbook.ActiveSheet.SaveAs Filename:= _
"c:\" & Range("B1"), FileFormat:=xlText, _
CreateBackup:=False
End Sub

Then I ran it as a macro. Do I need to change or add something else?

Thanks,
Ian

"Stefi" wrote:

Substitute "pathname" with your real pathname!

Sub txtsave()
ActiveWorkbook.ActiveSheet.SaveAs Filename:= _
"pathname" & Range("B1"), FileFormat:=xlText, _
CreateBackup:=False
End Sub


Regards,
Stefi

€žBigIan€ť ezt Ă*rta:

Is it possible to make an external file from the contents of a cell? For
example if I have "Text acbd" as the contents of cell A1, can I write it to a
new file called Text1.txt?
If so, can the file name Text1.txt be in another cell, B1 for example?
So, I've got A1 = "Text abcd", B1 = Text1.txt, is there a command or VB
routine that I can use to make a file called Txt1.txt with the contents "Text
abcd"?

Thanks,
Ian



  #6   Report Post  
Duke Carey
 
Posts: n/a
Default Write cell info to external file

Her code simply saves the active sheet as a text file.

Try this instead

Sub WriteCellToFile()
Dim fso As Object
Dim ts As Object
Dim strCellContents As String
Dim strFileName As String
Dim strPath As String

strCellContents = Range("A1").Text
strFileName = Range("B1").Text
strPath = Range("C1").Text

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile(strPath & strFileName, True)
ts.writeline (strCellContents)
ts.Close

End Sub

Change this line:

Set ts = fso.CreateTextFile(strPath & strFileName, True)

to

Set ts = fso.CreateTextFile(strPath & strFileName, False)

if you want to be sure to NOT OVERWRITE existing files
  #7   Report Post  
BigIan
 
Posts: n/a
Default Write cell info to external file

Hi Stefi,

It worked! I think the problem was that I hadn't set up the module bit in VB
correctly, your code was fine.

Thanks for your help,
Ian


"Stefi" wrote:

What version do you use? I tested it in XL2000 and it worked!
Nonetheless try this:

Sub txtsave()
ActiveWorkbook.ActiveSheet.SaveAs Filename:= _
"c:\" & Range("B1"), FileFormat:=xlTextWindows, _
CreateBackup:=False
End Sub

Regards,
Stefi

€žBigIan€ť ezt Ă*rta:

Thanks for your help Stefi, I can't get it to work though. I get a 400 error
when I run it.
I'm not very well up in VB so this is just a guess, but does your code just
define the filename? I see pathname + filename from cell B1 but I don't see
where the cell content of A1 goes.
What I've done so far is to copy your code exactly on to a VB code box and
replace "pathname" with "c:\" (I left the quotation marks in) so it looks
like this:

Sub txtsave()
ActiveWorkbook.ActiveSheet.SaveAs Filename:= _
"c:\" & Range("B1"), FileFormat:=xlText, _
CreateBackup:=False
End Sub

Then I ran it as a macro. Do I need to change or add something else?

Thanks,
Ian

"Stefi" wrote:

Substitute "pathname" with your real pathname!

Sub txtsave()
ActiveWorkbook.ActiveSheet.SaveAs Filename:= _
"pathname" & Range("B1"), FileFormat:=xlText, _
CreateBackup:=False
End Sub


Regards,
Stefi

€žBigIan€ť ezt Ă*rta:

Is it possible to make an external file from the contents of a cell? For
example if I have "Text acbd" as the contents of cell A1, can I write it to a
new file called Text1.txt?
If so, can the file name Text1.txt be in another cell, B1 for example?
So, I've got A1 = "Text abcd", B1 = Text1.txt, is there a command or VB
routine that I can use to make a file called Txt1.txt with the contents "Text
abcd"?

Thanks,
Ian

  #8   Report Post  
BigIan
 
Posts: n/a
Default Write cell info to external file

That's perfect Duke, just what I needed.

Thanks a lot,
Ian

"Duke Carey" wrote:

Her code simply saves the active sheet as a text file.

Try this instead

Sub WriteCellToFile()
Dim fso As Object
Dim ts As Object
Dim strCellContents As String
Dim strFileName As String
Dim strPath As String

strCellContents = Range("A1").Text
strFileName = Range("B1").Text
strPath = Range("C1").Text

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile(strPath & strFileName, True)
ts.writeline (strCellContents)
ts.Close

End Sub

Change this line:

Set ts = fso.CreateTextFile(strPath & strFileName, True)

to

Set ts = fso.CreateTextFile(strPath & strFileName, False)

if you want to be sure to NOT OVERWRITE existing files

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Links picking up values from an older version of linked file Cate Links and Linking in Excel 4 October 20th 05 01:53 PM
How do I use a cell value as the filename in an external link? wattkisson Excel Worksheet Functions 1 July 8th 05 11:51 PM
Possible Lookup Table Karen Excel Worksheet Functions 5 June 8th 05 09:43 PM
How can you import external text file paragraph a into single cell Sam Excel Discussion (Misc queries) 0 April 14th 05 12:58 PM
External link using calculated cell value Doug Excel Worksheet Functions 1 March 15th 05 08:05 AM


All times are GMT +1. The time now is 06:34 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"