ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro for write a txt file (https://www.excelbanter.com/excel-programming/432976-macro-write-txt-file.html)

japfvg

Macro for write a txt file
 
Hi all,

Thanks to you all I've been improving in VBA so thanks again and great this
forums exist.

Now I have a doubt and I'm not sure how to do it, I want to write a txt file
with the information of some cells separating them with commas. I have
already use the worksheets.saveas function, the text file is generated
correctly but I llike to see if there is any other option, maybe like the
open function in order to write only the txt file.

Do anyone have any idea for doing this?

Thanks again

Rick Rothstein

Macro for write a txt file
 
Are your cells in a contiguous range or are they scattered all around the
worksheet? If scattered all around, is there a regular pattern to them? It
would help if you could tell us the range you are wanting to save.

--
Rick (MVP - Excel)


"japfvg" wrote in message
...
Hi all,

Thanks to you all I've been improving in VBA so thanks again and great
this
forums exist.

Now I have a doubt and I'm not sure how to do it, I want to write a txt
file
with the information of some cells separating them with commas. I have
already use the worksheets.saveas function, the text file is generated
correctly but I llike to see if there is any other option, maybe like the
open function in order to write only the txt file.

Do anyone have any idea for doing this?

Thanks again



Mike H

Macro for write a txt file
 
Hi,

You aren't clear about what you want to write but you can create a text file
like this

Sub write_Texts()
Dim SomeName As String
SomeName = "MyTextFile"
saveDir = "C:\" 'Change to suit
targetfile = saveDir & SomeName & ".txt"

Open targetfile For Output As #1
Print #1, Range("A1").Text

Close #1
End Sub

Mike

"japfvg" wrote:

Hi all,

Thanks to you all I've been improving in VBA so thanks again and great this
forums exist.

Now I have a doubt and I'm not sure how to do it, I want to write a txt file
with the information of some cells separating them with commas. I have
already use the worksheets.saveas function, the text file is generated
correctly but I llike to see if there is any other option, maybe like the
open function in order to write only the txt file.

Do anyone have any idea for doing this?

Thanks again


Sam Wilson

Macro for write a txt file
 
Hi, this should give you an idea of the basics:

Sub demo()

Dim fs As Object
Dim a As Object

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.createtextfile("C:\demo.txt", True)

a.writeline ("line1, blah blah blah")
a.writeline ("line2, blah blah blah blah")

a.Close

End Sub

It creates a 2 line text file in root C etc.
"japfvg" wrote:

Hi all,

Thanks to you all I've been improving in VBA so thanks again and great this
forums exist.

Now I have a doubt and I'm not sure how to do it, I want to write a txt file
with the information of some cells separating them with commas. I have
already use the worksheets.saveas function, the text file is generated
correctly but I llike to see if there is any other option, maybe like the
open function in order to write only the txt file.

Do anyone have any idea for doing this?

Thanks again


japfvg

Macro for write a txt file
 
Yes, you're right. Is a range like A1:D10 where I have the info to export.

Thanks

"Rick Rothstein" wrote:

Are your cells in a contiguous range or are they scattered all around the
worksheet? If scattered all around, is there a regular pattern to them? It
would help if you could tell us the range you are wanting to save.

--
Rick (MVP - Excel)


"japfvg" wrote in message
...
Hi all,

Thanks to you all I've been improving in VBA so thanks again and great
this
forums exist.

Now I have a doubt and I'm not sure how to do it, I want to write a txt
file
with the information of some cells separating them with commas. I have
already use the worksheets.saveas function, the text file is generated
correctly but I llike to see if there is any other option, maybe like the
open function in order to write only the txt file.

Do anyone have any idea for doing this?

Thanks again




Patrick Molloy[_2_]

Macro for write a txt file
 

instead of this
Print #1, Range("A1").Text
which is quiet correct, you could do this:

for each cell in Selection | Range("A1:H10")
Print #1, cell.Text
next

"Mike H" wrote:

Hi,

You aren't clear about what you want to write but you can create a text file
like this

Sub write_Texts()
Dim SomeName As String
SomeName = "MyTextFile"
saveDir = "C:\" 'Change to suit
targetfile = saveDir & SomeName & ".txt"

Open targetfile For Output As #1
Print #1, Range("A1").Text

Close #1
End Sub

Mike

"japfvg" wrote:

Hi all,

Thanks to you all I've been improving in VBA so thanks again and great this
forums exist.

Now I have a doubt and I'm not sure how to do it, I want to write a txt file
with the information of some cells separating them with commas. I have
already use the worksheets.saveas function, the text file is generated
correctly but I llike to see if there is any other option, maybe like the
open function in order to write only the txt file.

Do anyone have any idea for doing this?

Thanks again


Mike H

Macro for write a txt file
 
I know and would have been more specific had the OP been more specific

Mike

"Patrick Molloy" wrote:


instead of this
Print #1, Range("A1").Text
which is quiet correct, you could do this:

for each cell in Selection | Range("A1:H10")
Print #1, cell.Text
next

"Mike H" wrote:

Hi,

You aren't clear about what you want to write but you can create a text file
like this

Sub write_Texts()
Dim SomeName As String
SomeName = "MyTextFile"
saveDir = "C:\" 'Change to suit
targetfile = saveDir & SomeName & ".txt"

Open targetfile For Output As #1
Print #1, Range("A1").Text

Close #1
End Sub

Mike

"japfvg" wrote:

Hi all,

Thanks to you all I've been improving in VBA so thanks again and great this
forums exist.

Now I have a doubt and I'm not sure how to do it, I want to write a txt file
with the information of some cells separating them with commas. I have
already use the worksheets.saveas function, the text file is generated
correctly but I llike to see if there is any other option, maybe like the
open function in order to write only the txt file.

Do anyone have any idea for doing this?

Thanks again


Patrick Molloy[_2_]

Macro for write a txt file
 
Option Explicit

Sub pushdata()
Dim cell As Range
Dim txt As String

For Each cell In Range("C5:D21")
txt = txt & "," & cell.Text
Next
Open "C:\temp\data.txt" For Output As #1
Print #1, Mid(txt, 2)
Close #1

End Sub


"japfvg" wrote:

Yes, you're right. Is a range like A1:D10 where I have the info to export.

Thanks

"Rick Rothstein" wrote:

Are your cells in a contiguous range or are they scattered all around the
worksheet? If scattered all around, is there a regular pattern to them? It
would help if you could tell us the range you are wanting to save.

--
Rick (MVP - Excel)


"japfvg" wrote in message
...
Hi all,

Thanks to you all I've been improving in VBA so thanks again and great
this
forums exist.

Now I have a doubt and I'm not sure how to do it, I want to write a txt
file
with the information of some cells separating them with commas. I have
already use the worksheets.saveas function, the text file is generated
correctly but I llike to see if there is any other option, maybe like the
open function in order to write only the txt file.

Do anyone have any idea for doing this?

Thanks again





All times are GMT +1. The time now is 08:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com