nigelandrewfoster
Well-known Member
- Joined
- May 27, 2009
- Messages
- 747
Hiya
I'm using the following function to open a UTF-8 csv file and save it safely as an xlsx file. I use this to preserve the foreign characters from corruption (which happens when I open it directly in Excel):
It works great ... until I encounter an exported CSV that contains commas WITHIN a field (eg. a text field), then it of course splits the line at that point. I cannot see any way around this. How CAN one parse a csv file when a field in that file contains commas AS commas, rather than as a delimiting symbol? I guess the answer is that programmers shouldn't permit fields of csv files to contain (text) commas without some kind of wrapping? I'm talking about the file exports of some sizeable companies.
I'm using the following function to open a UTF-8 csv file and save it safely as an xlsx file. I use this to preserve the foreign characters from corruption (which happens when I open it directly in Excel):
Code:
Function wsUCF8_CSV_Worksheet(strPathFilename As String, Optional strDestinationFilename As String) As Worksheet ' wb is set to the opened workbook
Dim wb As Workbook
Set wb = Workbooks.Add
Set wsUCF8_CSV_Worksheet = wb.Worksheets(1)
With wsUCF8_CSV_Worksheet
With .QueryTables.Add(Connection:="TEXT;" & strPathFilename, Destination:=.Range("A1"))
.TextFileParseType = xlDelimited
.TextFilePlatform = 65001
.TextFileCommaDelimiter = True
.Refresh
End With
End With
Remove_Workbook_Connections wb
If Not IsMissing(strDestinationFilename) Then
MakePath "C:\Temp"
wb.SaveAs "C:\Temp\" & strDestinationFilename, FileFormat:=xlWorkbookDefault
End If
End Function
It works great ... until I encounter an exported CSV that contains commas WITHIN a field (eg. a text field), then it of course splits the line at that point. I cannot see any way around this. How CAN one parse a csv file when a field in that file contains commas AS commas, rather than as a delimiting symbol? I guess the answer is that programmers shouldn't permit fields of csv files to contain (text) commas without some kind of wrapping? I'm talking about the file exports of some sizeable companies.
Last edited: