Sub GetCsvData(ByVal Link As String, ByVal year, month As Integer)
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
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
' file name is yy-mm-dd.csv
If CInt(Left(l, 2)) = year And CInt(Mid(l, 4, 2)) = month 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 [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
Pass = 0
NG = 0
Total = 0
LastRow = Cells(Rows.Count, "O").End(xlUp).Row
For n = 9 To LastRow
If Cells(n, 15) = "PASS" Then
Pass = Pass + 1
End If
If Cells(n, 15) = "FAIL" Then
NG = NG + 1
End If
Total = Pass + NG
Next
Cells(stt + 5, 6) = NG
Cells(stt + 5, 7) = Total
'Workbooks(toolsname).Sheets("LINK FOLDER").Cells(stt + 5, 5) = NG
'Workbooks(toolsname).Sheets("LINK FOLDER").Cells(stt + 5, 6) = Total
'Purpose is calculate how many PASS and FAIL each file csv
End If
Application.DisplayAlerts = False
Columns("O:O").Select
ActiveWindow.ScrollColumn = 209
Columns("O:HS").Select
Selection.ClearContents
Next
' I try to add this command to speed up code
Application.DisplayAlerts = False
End If
eh:
End Sub