Sub GetData()
Application.ScreenUpdating = False
Dim fso As FileSystemObject
Dim pth As String
Dim fl As File
Dim wb As Workbook
Set fso = New FileSystemObject
pth = "Yourfilepathhere"
For Each fl In fso.GetFolder(pth).Files
If StrComp(fso.GetExtensionName(fl.Path), "csv", vbTextCompare) = 0 Then
Set wb = Workbooks.Open(fl.Path)
wb.SaveAs pth & "\" & fso.GetBaseName(fl.Path), xlOpenXMLWorkbook
wb.Close
End If
Next
Set fl = Nothing
Set fso = Nothing
Dim cnStr, query, fileName As String
Dim rs As ADODB.Recordset
Dim Str As Long
fileName = "Yourfilepathhere\Issued_Stock_Report.xlsx"
cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & fileName & ";" & _
"Extended Properties=Excel 12.0"
Str = Sheet1.Range("A2").Value
query = "SELECT Distinct [Purchase] FROM [Issued_Stock_Report$] WHERE [Product]= " & Str
Set rs = New ADODB.Recordset
rs.Open query, cnStr, adOpenUnspecified, adLockUnspecified
If Not rs.EOF Then
ActiveSheet.Range("E2").CopyFromRecordset rs
Else
MsgBox "No records returned.", vbCritical
End If
rs.Close
Set cnStr = Nothing
Set rs = Nothing
Kill fileName
Dim i, j As Long
Dim s As String
i = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
For j = 2 To i
s = s & Sheet1.Cells(j, 5)
Next
Sheet1.Range("E2") = "'" & s
AddCommas
Sheet1.Range("E3:E" & i).ClearContents
Application.ScreenUpdating = False
End Sub
Sub AddCommas()
Dim s As String
s = Sheet1.Range("E2")
Dim x As Long
x = Len(s) \ 3
If Len(s) Mod 3 = 0 Then
x = x - 1
End If
Do Until x <= 0
s = Left(s, x * 3) & "," & Mid(s, x * 3 + 1)
x = x - 1
Loop
Sheet1.Range("E2") = s
End Sub