View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
NickHK NickHK is offline
external usenet poster
 
Posts: 4,391
Default How to export a range of cells on a worksheet to a text file

For fun, here's another way. As Tom says, depends if this format is
suitable.

Private Sub CommandButton1_Click()
Dim DatObj As DataObject
Dim FileNum As Long
Dim TempStr As String

Const TEXTFILE As String = "C:\routes.txt"
Const TEXTFORMAT As Long = 1

Range("A1:F20").Copy

Set DatObj = New DataObject
With DatObj
.GetFromClipboard
If .GetFormat(TEXTFORMAT) = True Then
FileNum = FreeFile
TempStr = .GetText(TEXTFORMAT)
'Seem to get an extra vbCrLf from the DataObject, so remove
TempStr = Left(TempStr, Len(TempStr) - 2)

Open TEXTFILE For Output As #FileNum
Print #FileNum, TempStr
Close #FileNum
End If
End With

Application.CutCopyMode = False

End Sub

NickHK

wrote in message
oups.com...
Can anyone describe how to export the values in range A1 to F20 on
worksheet 'Data' to a text file? The text file will be named
'routes.txt'. The text file already exists, and it should be
overwritten with the new values each time the macro is executed,
rather than the macro appending the values to the text file.

Thanks kindly.

Best Regards,
Joseph