Saving a Worksheet as a Text File?


Posted by Christopher R. Patrick on November 16, 2001 11:25 AM

Hi all-

Does anyone know if it is possible to save a worksheet as a text file, but with a delimeter other than a comma? I know I can save as a CSV or such, but I'm really wanting to take data from a worksheet, save it to a file with my own delimeter. Is that possible?

Thanks for any help

chris

Posted by Dank on November 16, 2001 12:22 PM


Hi Chris,

How about this code? It allows you to select any delimiter you want and then writes the text file. Let me know if you have any problems.

Regards,
Daniel.


Sub SaveCustomDelimiter()
Dim strDelim As String, strFilePath As String, intFileNum As Integer
Dim vWriteData, lngRow As Long, lngCol As Long

Do
strDelim = InputBox("Please enter required delimiter.", "Delimiter")

If strDelim = "" Then Exit Sub 'User pressed cancel

If Len(strDelim) > 1 Then
MsgBox "Delimiter can only be one character long.", vbOKOnly, "Invalid delimiter"
End If
Loop Until Len(strDelim) = 1

'Now open a file and write the active sheet data to it
strFilePath = "C:\temp\anyname.txt"
intFileNum = FreeFile

Open strFilePath For Output As intFileNum

'Assign the active sheet data to an array to increase speed
vWriteData = ActiveSheet.UsedRange

For lngRow = 1 To ActiveSheet.UsedRange.Rows.Count
For lngCol = 1 To ActiveSheet.UsedRange.Columns.Count
Print #intFileNum, vWriteData(lngRow, lngCol); strDelim;
Next lngCol
Print #intFileNum,
Next lngRow

Close #intFileNum

End Sub



Posted by Christopher R. Patrick on November 16, 2001 12:35 PM

Awesome....exactly what I was looking for...thank you so much.

Chris