Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default vba to append to a text file

I have cells like these:
A2
A3
A4
....
A54
which are not all 53 char in length.
I'd like to append only the content of A2 to A54 that is 53 char in length
to a text file.
Thx.
cg
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default vba to append to a text file

This code should do what you want...

Sub AppendText53()
Dim X As Long
Dim FF As Long
Dim Rnge As Range
Dim FileName As String
FileName = "C:\TEMP\Test.txt"
FF = FreeFile()
Open FileName For Append As #FF
For Each Rnge In Range("A2:A54")
If Len(Rnge.Value) = 53 Then Print #FF, Rnge.Value
Next
Close #FF
End Sub

Place it in the code window for the sheet you want to run it from and change the example filename I used to the filename of the actual file you want to append to. Of course, as you should always do with code you get from others, test it out on a sample file before applying it to your real file.

Rick


"hccatmo" wrote in message ...
I have cells like these:
A2
A3
A4
...
A54
which are not all 53 char in length.
I'd like to append only the content of A2 to A54 that is 53 char in length
to a text file.
Thx.
cg

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default vba to append to a text file

Thanks, Rick. I forget to mention that I'd like to have a new line for each
cell content.
Will try and keep you posted.
cg

"Rick Rothstein (MVP - VB)" wrote:

This code should do what you want...

Sub AppendText53()
Dim X As Long
Dim FF As Long
Dim Rnge As Range
Dim FileName As String
FileName = "C:\TEMP\Test.txt"
FF = FreeFile()
Open FileName For Append As #FF
For Each Rnge In Range("A2:A54")
If Len(Rnge.Value) = 53 Then Print #FF, Rnge.Value
Next
Close #FF
End Sub

Place it in the code window for the sheet you want to run it from and change the example filename I used to the filename of the actual file you want to append to. Of course, as you should always do with code you get from others, test it out on a sample file before applying it to your real file.

Rick


"hccatmo" wrote in message ...
I have cells like these:
A2
A3
A4
...
A54
which are not all 53 char in length.
I'd like to append only the content of A2 to A54 that is 53 char in length
to a text file.
Thx.
cg


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 897
Default vba to append to a text file

I think you would change it to

If Len(Rnge.Value) = 53 Then Print #FF, Rnge.Value, ""

but let's see if anyone else is able to assist.


HTH,
JP

On Jan 11, 8:08*am, hccatmo wrote:
Thanks, Rick. I forget to mention that I'd like to have a new line for each
cell content.
Will try and keep you posted.
cg

"Rick Rothstein (MVP - VB)" wrote:



This code should do what you want...


Sub AppendText53()
* Dim X As Long
* Dim FF As Long
* Dim Rnge As Range
* Dim FileName As String
* FileName = "C:\TEMP\Test.txt"
* FF = FreeFile()
* Open FileName For Append As #FF
* For Each Rnge In Range("A2:A54")
* * If Len(Rnge.Value) = 53 Then Print #FF, Rnge.Value
* Next
* Close #FF
End Sub


Place it in the code window for the sheet you want to run it from and change the example filename I used to the filename of the actual file you want to append to. Of course, as you should always do with code you get from others, test it out on a sample file before applying it to your real file.


Rick


"hccatmo" wrote in ...
I have cells like these:
A2
A3
A4
...
A54
which are not all 53 char in length.
I'd like to append only the content of A2 to A54 that is 53 char in length
to a text file.
Thx.
cg- Hide quoted text -


- Show quoted text -


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default vba to append to a text file

I assumed that... the code I posted already puts each cell's content onto it own line in the file it is appending to.

Rick


"hccatmo" wrote in message ...
Thanks, Rick. I forget to mention that I'd like to have a new line for each
cell content.
Will try and keep you posted.
cg

"Rick Rothstein (MVP - VB)" wrote:

This code should do what you want...

Sub AppendText53()
Dim X As Long
Dim FF As Long
Dim Rnge As Range
Dim FileName As String
FileName = "C:\TEMP\Test.txt"
FF = FreeFile()
Open FileName For Append As #FF
For Each Rnge In Range("A2:A54")
If Len(Rnge.Value) = 53 Then Print #FF, Rnge.Value
Next
Close #FF
End Sub

Place it in the code window for the sheet you want to run it from and change the example filename I used to the filename of the actual file you want to append to. Of course, as you should always do with code you get from others, test it out on a sample file before applying it to your real file.

Rick


"hccatmo" wrote in message ...
I have cells like these:
A2
A3
A4
...
A54
which are not all 53 char in length.
I'd like to append only the content of A2 to A54 that is 53 char in length
to a text file.
Thx.
cg




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default vba to append to a text file

It works well, as expected. Thx. cg

"Rick Rothstein (MVP - VB)" wrote:

This code should do what you want...

Sub AppendText53()
Dim X As Long
Dim FF As Long
Dim Rnge As Range
Dim FileName As String
FileName = "C:\TEMP\Test.txt"
FF = FreeFile()
Open FileName For Append As #FF
For Each Rnge In Range("A2:A54")
If Len(Rnge.Value) = 53 Then Print #FF, Rnge.Value
Next
Close #FF
End Sub

Place it in the code window for the sheet you want to run it from and change the example filename I used to the filename of the actual file you want to append to. Of course, as you should always do with code you get from others, test it out on a sample file before applying it to your real file.

Rick


"hccatmo" wrote in message ...
I have cells like these:
A2
A3
A4
...
A54
which are not all 53 char in length.
I'd like to append only the content of A2 to A54 that is 53 char in length
to a text file.
Thx.
cg


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
Append Excel Sheet from constantly updating delimited text file Ker G Setting up and Configuration of Excel 1 April 8th 08 09:33 PM
Append text file Jeff Excel Discussion (Misc queries) 6 October 8th 07 05:56 PM
Append text Jeff Excel Discussion (Misc queries) 1 February 16th 07 10:28 PM
Append Records to a Text File Randy[_10_] Excel Programming 6 December 3rd 05 06:40 AM
excel97 vba to append lines to text file overwriting last 2 lines Paul Excel Programming 1 November 6th 04 08:11 PM


All times are GMT +1. The time now is 05:21 PM.

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

About Us

"It's about Microsoft Excel"