Below code Works for TAB delimeter but if there is ' ,' in a row its not reading correct data.Basically I am trying to read multiple .txt files to .csv .Any help appreciated...Thank you...
Sub txt2csv()
Dim Fname As String, ipath As String, retstring, fs, a, i As Long
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = -1 Then ipath = .SelectedItems(1) Else Exit Sub
End With
Application.ScreenUpdating = False: On Error Resume Next
Fname = Dir(ipath & "\*.txt")
Do While Fname <> ""
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(ipath & "\" & Fname, 1, 0)
'retstring = Replace(a.readall, vbTab, " " & Replace(Fname, ".txt", "") & "," & vbTab) & "," & Replace(Fname, ".txt", "") & ",": a.Close
retstring = Replace(a.readall, vbTab, " " & Replace("", ".txt", "") & "," & "") & "," & Replace(Fname, ".txt", "") & ",": a.Close
If Err.Number = 0 Then
Open ipath & "\" & Fname For Output As #1
Print #1, retstring
Close #1
Name ipath & "\" & Fname As ipath & "\" & Replace(Fname, "txt", "csv")
Else
Err.Clear
End If
Fname = Dir
Loop
Set fs = Nothing: Application.ScreenUpdating = False
End Sub
Bookmarks