Norie
asked on
Strange field name when exporting from Excel to CSV.
I've been exporting data from Excel to CSV manually using Save As....
The intention is to then use ADO to query the data in the CSV files and everything is fine apart from one thing - the first field name is being prefixed with some strange characters.
For example, if the first field/column in Excel is SiteName then it's appearing in the CSV file as SiteName
This is obviously causing problems when it comes to queries as I can't use SiteName in the field list or criteria.
Does anybody know why this is happening?
PS I know there are other ways to get the data into CSV format, and I'll probably end up using one of them, but I'm kind of curious to why this is happening and whether, from within Excel, there is
The intention is to then use ADO to query the data in the CSV files and everything is fine apart from one thing - the first field name is being prefixed with some strange characters.
For example, if the first field/column in Excel is SiteName then it's appearing in the CSV file as SiteName
This is obviously causing problems when it comes to queries as I can't use SiteName in the field list or criteria.
Does anybody know why this is happening?
PS I know there are other ways to get the data into CSV format, and I'll probably end up using one of them, but I'm kind of curious to why this is happening and whether, from within Excel, there is
Is the heading simply the word SiteName with no special formatting like perhaps a merged cell, or any formatting at all?
When you go to save it do you get a message concerning non-compatible features?
ASKER
The header was bold but even after I remove that formatting I'm still getting the strange characters.
No message about compatible features.
No message about compatible features.
Copy the data to a CSV file instead, so copy everything, open a new workbook, paste, then save as CSV.
That should work, if not, open the CSV in notepad, change the heading and resave it.
That should work, if not, open the CSV in notepad, change the heading and resave it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Alex
I opened the CSV in Notepad and the characters aren't there.
I've attached a sample file and here's the code I'm using to query it, which has been recycled from another project.
I opened the CSV in Notepad and the characters aren't there.
I've attached a sample file and here's the code I'm using to query it, which has been recycled from another project.
Option Explicit
Sub ConsolidateWithADO()
Dim wsData As Worksheet
Dim conn As Object
Dim cat As Object
Dim rst As Object
Dim tbl As Object
Dim fld As Object
Dim strPath As String
Dim strSQL As String
Application.ScreenUpdating = False
Set wsData = Sheets.Add(After:=Sheets(Sheets.Count))
'wsData.Name = "Consolidated Data"
strPath = ThisWorkbook.Path
Set conn = CreateObject("ADODB.Connection")
Set cat = CreateObject("ADOX.Catalog")
' set connection to folder with CSV files
conn.Connectionstring = "Provider=Microsoft.ACE.OLEDB.16.0;Data Source=" & strPath & _
";Extended Properties=""text;HDR=Yes;"";Persist Security Info=False"
conn.Open
cat.ActiveConnection = conn
Set rst = CreateObject("ADODB.Recordset")
' loop through each CSV in folder and get data
For Each tbl In cat.Tables
' get data from CSV file
strSQL = "SELECT * FROM [" & tbl.Name & "] W"
rst.Open strSQL, conn
With wsData
' if no field names, add them
If .Range("A1").Value = "" Then
For Each fld In rst.Fields
.Cells(1, Columns.Count).End(xlToLeft).Offset(, 1).Value = fld.Name
Next fld
.Columns(1).Delete
End If
' copy data from CSV to next empty row
.Range("A" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset rst
End With
rst.Close
Next tbl
' clean up
Set rst = Nothing
Set cat = Nothing
conn.Close
Set conn = Nothing
' tidy up results a bit
wsData.Cells.EntireColumn.AutoFit
Application.ScreenUpdating = True
End Sub
EE-Sample-Data.xlsx
I don't see "Sitename" in there at all
Notepad++ not Notepad
It'll work in Notepad too.
ASKER
Oops, do I feel embarrassed.
Did the export again and instead of picking the first CSV option. CSV UTF-8, from the Save as type drop down I picked the one I normally use CSV.
Everything appears to be fine now.:)
Did the export again and instead of picking the first CSV option. CSV UTF-8, from the Save as type drop down I picked the one I normally use CSV.
Everything appears to be fine now.:)