View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default txt tab-delimited to CSV

Maybe something like this:

Option Explicit
Sub testme01()

Dim myFileNames As Variant
Dim myArray() As Variant
Dim iCtr As Long
Dim maxFields As Long
Dim wkbk As Workbook
Dim NewFileName As String

maxFields = Worksheets(1).Columns.Count

myFileNames = Application.GetOpenFilename _
(filefilter:="Text Files, *.txt", MultiSelect:=True)

If IsArray(myFileNames) = False Then
Exit Sub 'user hit cancel
End If

ReDim myArray(1 To maxFields, 1 To 2)
For iCtr = 1 To maxFields
myArray(iCtr, 1) = iCtr
'bring it in as text--so nothing changes
myArray(iCtr, 2) = xlTextFormat
Next iCtr

For iCtr = LBound(myFileNames) To UBound(myFileNames)
Workbooks.OpenText Filename:=myFileNames(iCtr), _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, _
FieldInfo:=myArray

If LCase(Right(myFileNames(iCtr), 4)) = ".txt" Then
NewFileName = Left(myFileNames(iCtr), Len(myFileNames(iCtr)) - 4)
End If
NewFileName = NewFileName & ".csv"

Set wkbk = ActiveWorkbook

'overwrite any existing .csv file
Application.DisplayAlerts = False
wkbk.SaveAs Filename:=NewFileName, FileFormat:=xlCSV
Application.DisplayAlerts = True

wkbk.Close savechanges:=False

Next iCtr

End Sub

wrote:

We use a program which is only able to export data into 'tab-
delimited' text files. We need data from this program to go to
another which can only read CSV files.

Is there a way to script this so it automatically converts the
contents of a text file to a csv which can be opened in excel?

Thanks


--

Dave Peterson