'===========================================================================
'- MACRO TO CONVERT WORKSHEET TO PIPE OR TAB (or other) DELIMITED TEXT FILE
'- WS HAS TO BE SET UP AS A SIMPLE TABLE. RUN MACRO FROM THE SHEET
'- Brian Baulsom April 2002
'===========================================================================
'-
Sub EXPORT_SHEET_TO_TEXT()
Dim FileName As String ' .txt FILENAME AS WORKSHEET
Dim MyRow As Long
Dim MyCol As Integer
Dim MyReturn As String
Dim ws As Worksheet
Dim LastRow As Long
Dim ColumnCount As Integer
Dim MyDelimiter As String
'---------------------------------------------------------------------
Application.Calculation = xlCalculationManual
'- set the delimiter required
'MyDelimiter = "|" ' pipe
MyDelimiter = Chr(9) ' tab
'---------------------------------------------------------------------
Set ws = ActiveSheet
MyReturn = Chr(13)
FileName = ws.Name & ".txt"
'----------------------------------------------------------------------
'- get number of rows (to allow for blank cells)
LastRow = ws.Cells.Find(What:="*", SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
'----------------------------------------------------------------------
'- get number of columns
ColumnCount = ws.Cells.Find(What:="*", SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
'----------------------------------------------------------------------
'- EXPORT TO FILE
Open FileName For Append As #1
'- loop through rows & columns
For MyRow = 1 To LastRow
For MyCol = 1 To ColumnCount
Print #1, ws.Cells(MyRow, MyCol).Value;
If MyCol < ColumnCount Then Print #1, MyDelimiter; ' Column
Next
Print #1, MyReturn; ' end of line Return
Next
'----------------------------------------------------------------------
Close #1
MsgBox ("Done")
Application.Calculation = xlCalculationAutomatic
End Sub
'=========== END OF PROCEDURE =============================================