Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 866
Default copy range to Text (Tab delimited) (*.txt) file

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default copy range to Text (Tab delimited) (*.txt) file


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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default copy range to Text (Tab delimited) (*.txt) file

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" wrote in message
...
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
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
Converting Tab Delimited Text File to A Comma Delimited Text File Dave Peterson Excel Programming 0 June 13th 07 03:13 PM
Saving multi-tab excel file created from comma delimited text file Marcus Aurelius Excel Programming 2 December 19th 05 05:16 PM
How can I save a file as a comma-delimited text file in Excel? LAM Excel Discussion (Misc queries) 1 May 3rd 05 10:24 PM
Export excel file to semicolon delimited text file capitan Excel Discussion (Misc queries) 5 April 7th 05 03:06 AM
Open delimited text file to excel without changing data in that file zohanc Excel Programming 1 October 3rd 03 01:06 AM


All times are GMT +1. The time now is 03:31 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"