hi all,
Now I make a tool to get some data from csv file (no need all data).
But my code may be can not run,
pls someone help me.
Now I make a tool to get some data from csv file (no need all data).
But my code may be can not run,
pls someone help me.
Code:
Sub GetCsvData(ByVal Link As String)
Dim ws As Worksheet
Dim fPath As String
Dim fso As Object
Dim mySource As Object, file1 As Variant
Dim myObject As Object
fPath = Link 'Range(E19) is link include my csv file
Dim cn As Object, rst As Object
Dim l As String
Dim stt, Pass, NG, Total, LastRow, n As Integer
Dim toolsname, u, Fder, Folder, strQuery
Set fso = CreateObject("Scripting.FileSystemObject")
Application.DisplayAlerts = False
toolsname = ThisWorkbook.Name
Fder = Link
u = Right(Fder, 1)
If u = "\" Then
Folder = Fder
Else
Folder = Fder & "\"
End If
If Fder = "" Then
Exit Sub
Else
Set mySource = fso.GetFolder(Folder)
For Each file1 In mySource.Files
l = Dir(file1)
'If CInt(Left(l, 2)) = year And CInt(Mid(l, 4, 2)) = month Then
'If CInt(Left(l, 2)) = year And CInt(Mid(l, 4, 2)) = month Then
If Right(l, 4) = ".csv" Then
'stt = CInt(Mid(l, 7, 2))
Set cn = CreateObject("ADODB.Connection")
'l = "16-08-11.csv" ' --> example l is file name of csv file
'l = file
On Error GoTo eh
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & fPath & ";" & _
"Extended Properties=""text;HDR=NO;FMT=Delimited;Imex=1;ImportMixedTypes=Text;"""
.CursorLocation = 1
.Open
End With
'strQuery = "SELECT * From [" & l & "]" ' ---> It can not open l
strQuery = "SELECT t2.[F1], t1.[F2], t1.[F3], t1.[F4], t1.[F5] From [" & l & "]"
'strQuery = "SELECT t2.[F1], t1.[F2], t1.[F3], t1.[F4], t1.[F5] From [16-08-11.csv] as t1 " & _
' "RIGHT OUTER JOIN [16-08-11.csv] as t2 " & _
' " ON t2.[F1] = t1.[F1];"
Set rst = CreateObject("ADODB.Recordset")
rst.Open strQuery, cn, 1, 3
Range("O1").CopyFromRecordset rst
rst.Close
cn.Close