With the help of this forum have been able to solve most problem but this has me stuck.
I have a comma delimited csv
file ("xxxx","zzz",) that has hidden chr(10)
and chr(13)
in the file. If I use a script to replace both these characters, I lose the end of record chr(10)
so only imports as one record.
In notepad the file shows perfect - one record per line. If I open as a an excel file it is ok, its only when I import as a csv delimited file
With thanks to other contributors, below is what I have been using.
Ideally what I would like to do is:
- select the csv file
- copy the file to keep the original <<< extra function
- clean up all hidden characters that would affect the import
- then import
-
Private Sub CSV_ImportRepl()
Dim strFile As String
Dim strBuffer As String
Dim ff As Integer
Dim strFileName As String
Dim ws As Worksheet
'ENTRIES CSV FILE ----------------------------------
' ---open file ----------
strFile = Application.GetOpenFilename("Text Files (*.csv),*.*", _
, "SELECT ENTRIES csv FILE")
strFileName = strFile
MsgBox strFileName
' ---start cleaning file ----------
strBuffer = Space(FileLen(strFile))
ff = FreeFile
Open strFile For Binary Access Read As #ff
Get #ff, , strBuffer
Close #ff
strBuffer = Replace(strBuffer, Chr(13), "")
Kill strFile
Open strFile For Binary Access Write As #ff
Put #ff, , strBuffer
Close #ff
' --- clear contents & import ----------
Sheets("Entries").Cells.ClearContents
Set ws = ActiveWorkbook.Sheets("Entries") 'set to current worksheet name
strFile = strFileName
With ws.QueryTables.Add(Connection:="TEXT;" & strFile, _
Destination:=ws.Range("A1"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh
End With
End Sub
Any help would be most appreciated.