Import CSV File VBA

Trevor G

Well-known Member
Joined
Jul 17, 2008
Messages
6,737
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
Hi I am using code to import a csv file but receive a message box stating this is a SYLK file, but cannot load it, either the file has errors (which it doesn't 4 rows with 4 columns all text entries) or it is not a SYLK file format.

Can anyone help get ride of this message box

My code is shown here

Option Compare Database<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Option Explicit<o:p></o:p>
<o:p> </o:p>
Function import1()<o:p></o:p>
Dim ws As Excel.Application<o:p></o:p>
Dim cnn As ADODB.Connection<o:p></o:p>
Dim rst As ADODB.Recordset<o:p></o:p>
<o:p> </o:p>
Set cnn = CurrentProject.Connection<o:p></o:p>
Set rst = New ADODB.Recordset<o:p></o:p>
Set ws = CreateObject("Excel.Application")<o:p></o:p>
ws.Workbooks.Open "F:\Data\tblPeeps.csv"<o:p></o:p>
rst.Open "tblPeeps", cnn, adOpenKeyset, adLockOptimistic, adCmdTableDirect<o:p></o:p>
ws.Sheets("tblpeeps").Select<o:p></o:p>
ws.Range("A2").Select<o:p></o:p>
Do Until ws.ActiveCell.Value = ""<o:p></o:p>
With rst<o:p></o:p>
.AddNew<o:p></o:p>
.Fields("Title").Value = ws.ActiveCell.Offset(0, 1).Value<o:p></o:p>
.Fields("FirstName").Value = ws.ActiveCell.Offset(0, 2).Value<o:p></o:p>
.Fields("LastName").Value = ws.ActiveCell.Offset(0, 3).Value<o:p></o:p>
.Fields("JobTitle").Value = ws.ActiveCell.Offset(0, 4).Value<o:p></o:p>
.Fields("Company").Value = ws.ActiveCell.Offset(0, 5).Value<o:p></o:p>
.Update<o:p></o:p>
End With<o:p></o:p>
ws.ActiveCell.Offset(1, 0).Select<o:p></o:p>
Loop<o:p></o:p>
MsgBox "Records have been added"<o:p></o:p>
<o:p> </o:p>
End Function

Thank you
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I have sorted this issue, you can't use ID in capitals in the csv file, changed that to lowercase and it works a treat.

Thank you for all who have viewed this thread.

Trevor
 
Upvote 0
Hello, I know this is a really old post, but if anyone runs across it, here is the VBA solution:

I was using a macro to convert text files to XLSX and one of the text files had "ID" in the first two characters. EXCEL automatically labels files like this as SYLK files and throws popups asking what you want to do about it (I know, it is ridiculous, but true). I tried a suggestion to convert the file to UTF-8, but then my pipe delimiter was not recognized. After trying may different things, I finally found a very simple solution.

I hope this helps those of you who have been pulling your hair out over this!

Application.DisplayAlerts = False
Workbooks.Open Filename:=folderName & "\" & myfile, Format:=6, Delimiter:="|"
Application.DisplayAlerts = True

Be simply adding the DisplayAlerts statements, it powered right through the popups and handled my file correctly.

Annette
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top