У меня есть код ниже, работающий со всеми типами файлов, кроме CSV-файлов.ADODB Connection пуст, не удается прочитать файл CSV
Я пытаюсь скопировать CSV-файл в рабочий лист, и я смутился. Вся помощь будет оценена.
отладчик показывает:
rsConn = ""
szSQL = "SELECT * FROM export.csv"
Когда я ударил
rsData.Open szSQL, rsCon, 0, 1, 1
Я иду в обработчик myError
ошибки
Я дистиллированную проблему вплоть до этих шагов, но ответ ускользает от меня ,
szSQL = "SELECT * FROM export.cvs"
Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")
rsCon.Open szConnect
rsData.Open szSQL, rsCon, 0, 1, 1
'These are the values being passed in.
GetData("export.csv" ,"A1:BE", "BirdFeet", "A1", "sku", True, True
Весь код здесь:
Public Sub GetData(SourceFile As Variant, SourceSheet As String, SourceRange As String, _
TargetSheet As String, TargetRange As String, _
TargetSortColumn As String, _
HaveHeader As Boolean, UseHeaderRow As Boolean)
Dim lColumn As Long
Dim lCount As Long
Dim lRow As Long
Dim rsCon As Object
Dim rsData As Object
Dim szConnect As String
Dim szSQL As String
lRow = Range(TargetRange).Row
lColumn = Range(TargetRange).Column
' Create the connection string.
If HaveHeader = False Then 'No there is NOT a header row.
If Val(Application.Version) < 12 Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=No"";"
Else
szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 12.0;HDR=No"";"
End If
Else 'Yes there is a Header Row
If Val(Application.Version) < 12 Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes"";"
Else
If (Right(SourceSheet, 4) = ".csv") Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""text; HDR=Yes; FMT=Delimited; IMEX=1;"""
Else
szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes"";"
End If
End If
End If
If SourceSheet = "" Then
'Create query strings
szSQL = "SELECT * FROM " & SourceRange$ & " ORDER BY sku;"
ElseIf SourceSheet = "DiamondAvian" Or SourceSheet = "export.csv" Then
szSQL = "SELECT * FROM export.csv"
Else
szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "] WHERE sku <> NULL ORDER BY " & TargetSortColumn & ";" 'THIS WORKS FOR DICIONARY
End If
On Error GoTo SomethingWrong
Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")
rsCon.Open szConnect
rsData.Open szSQL, rsCon, 0, 1, 1
' Check to make sure we received data and copy the data
If Not rsData.EOF Then
If HaveHeader = False Then
Cells(1, 1).CopyFromRecordset rsData
Else
'Add the header cell in each column if the last argument is True
If UseHeaderRow Then
For lCount = 0 To rsData.Fields.Count - 1 'Builds the Header row one column at a time.
Cells(lRow, lColumn + lCount).value = rsData.Fields(lCount).Name 'lcount determines the Column to paste header info in.
Next lCount
Cells(lRow + 1, lColumn).CopyFromRecordset rsData 'This is the step that copies and Pastes the data.
Else
Cells(lRow + 1, lColumn).CopyFromRecordset rsData
End If
End If
Else
MsgBox "No records returned from : " & SourceFile, vbCritical
End If
rsData.Close ' Clean up our Recordset object.
Set rsData = Nothing
rsCon.Close
Set rsCon = Nothing
Exit Sub
SomethingWrong:
MsgBox "The file name, Sheet name or Range is invalid of : " & SourceFile, vbExclamation, "Error"
On Error GoTo 0
End Sub
export.csv данные образца ------------------------------------------ -------------------------------------------------- ------------------------------ sku \t цена \t post_date \t post_date_gmt B1 - M \t \t 16.04.2011 11: 15 \t 4/19/2015 15:15 В8 \t 8,06 \t 4/19/2015 11:11 \t 4/19/2015 15:11 В1 \t \t 10,79 4/19/2015 11:08 \t 4/19/2015 15:08 B2 \t 11.65 \t 19.04.2011 11:08 \t 19.04.2005 15:08 В3 \t 11,98 \t 4/19/2015 11:08 \t 4/19/2015 15:08 B3B \t 12,74 \t 4/19/2015 11:08 \t 4/19/2015 15:08 В4 \t \t 16.24 4/19/2015 11:08 \t 4/19/2015 15:08 SB770 \t \t 4/3/2015 12:37 \t 4/3/2015 16:37 FM97 \t \t 7,52 3/24/2015 15:52 \t 24.03.2019 19:52 – CraigM