PC Review


Reply
Thread Tools Rate Thread

copy range to Text (Tab delimited) (*.txt) file

 
 
Dan
Guest
Posts: n/a
 
      14th Feb 2008
Hello,
I need to copy a range (A4:N20) to a text file in a folder.
The format of the file should be Text (Tab delimited) (*.txt) .
Can I do that via code.
Many thanks.
Dan
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      14th Feb 2008

Option Explicit
Sub testme()
Dim RngToCopy As Range
Dim NewWks As Worksheet

Set RngToCopy = ActiveSheet.Range("A4:N20")
Set NewWks = Workbooks.Add(1).Worksheets(1)

RngToCopy.Copy
NewWks.Range("a1").PasteSpecial Paste:=xlPasteValues

With NewWks.Parent
.SaveAs Filename:="C:\someexistingfolder\somename.txt", FileFormat:=xlText
.Close savechanges:=False
End With

End Sub

It creates a new workbook (single sheet) and copies the range to that sheet.
Then saves the new workbook as a text file.


Dan wrote:
>
> Hello,
> I need to copy a range (A4:N20) to a text file in a folder.
> The format of the file should be Text (Tab delimited) (*.txt) .
> Can I do that via code.
> Many thanks.
> Dan


--

Dave Peterson
 
Reply With Quote
 
RB Smissaert
Guest
Posts: n/a
 
      14th Feb 2008
Another way to do this:

Option Explicit

Sub Test()

Dim arr
Dim strFile As String

'replace this with suitable code
strFile = "C:\test.txt"

'replace this with suitable code
arr = ActiveWindow.RangeSelection

SaveArrayToText strFile, arr

End Sub


Sub SaveArrayToText(ByRef strFile As String, _
ByRef arr As Variant, _
Optional ByVal LBRow As Long = -1, _
Optional ByVal UBRow As Long = -1, _
Optional ByVal LBCol As Long = -1, _
Optional ByVal UBCol As Long = -1, _
Optional ByRef arrFields As Variant)

Dim r As Long
Dim c As Long
Dim hFile As Long

If LBRow = -1 Then
LBRow = LBound(arr, 1)
End If

If UBRow = -1 Then
UBRow = UBound(arr, 1)
End If

If LBCol = -1 Then
LBCol = LBound(arr, 2)
End If

If UBCol = -1 Then
UBCol = UBound(arr, 2)
End If

hFile = FreeFile

'Close before reopening in another mode.
'---------------------------------------
On Error Resume Next
Open strFile For Input As #hFile
Close #hFile

Open strFile For Output As #hFile

If IsMissing(arrFields) Then
For r = LBRow To UBRow
For c = LBCol To UBCol
If c = UBCol Then
Write #hFile, arr(r, c)
Else
Write #hFile, arr(r, c);
End If
Next c
Next r
Else
For c = LBCol To UBCol
If c = UBCol Then
Write #hFile, arrFields(c)
Else
Write #hFile, arrFields(c);
End If
Next c
For r = LBRow To UBRow
For c = LBCol To UBCol
If c = UBCol Then
Write #hFile, arr(r, c)
Else
Write #hFile, arr(r, c);
End If
Next c
Next r
End If

Close #hFile

End Sub


RBS


"Dan" <(E-Mail Removed)> wrote in message
news:BE1CCF92-7564-46D7-A9A1-(E-Mail Removed)...
> Hello,
> I need to copy a range (A4:N20) to a text file in a folder.
> The format of the file should be Text (Tab delimited) (*.txt) .
> Can I do that via code.
> Many thanks.
> Dan


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting Pipe Delimited Text File To Tab Delimited Text file Probie Coder Microsoft Access VBA Modules 5 3rd Dec 2009 05:56 AM
Japanese text lost when save as tab delimited text file Greg Lovern Microsoft Excel Programming 0 24th Oct 2007 08:39 PM
Converting Tab Delimited Text File to A Comma Delimited Text File kwesi.acquah@gmail.com Microsoft Excel Programming 1 13th Jun 2007 03:13 PM
Exporting Text with a data to a delimited text file =?Utf-8?B?RCBIYWZlciAtIFRGRQ==?= Microsoft Access External Data 4 5th Sep 2006 06:16 PM
Saving multi-tab excel file created from comma delimited text file =?Utf-8?B?TWFyY3VzIEF1cmVsaXVz?= Microsoft Excel Programming 2 19th Dec 2005 05:16 PM


Features
 

Advertising
 

Newsgroups
 


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