Hello there,
I'm new to VBA, I've got a book on the subject en route from amazon, and up until now I've been able to pull off what I need by searching the web and clipping parts of other folk's examples.
The code below does exactly what I need, but it takes a LOOOONG time...
Im searching for the two latest .csv's in a folder containing thousands of files, they have "-L" and "-R" in the file name, repectively. The file is copied into the destination folder, renamed, opened, closed, and deleted.
All of this is to refresh the data in the master sheet from which the sub is ran on a daily basis. Other than the -L and -R, the file names vary greatly depending on the person generating the .csv files in the source folder.
I would greatly appreciate any advice on how I could improve my process or maybe point me in the right direction when I begin studying a little deeper.
I'm new to VBA, I've got a book on the subject en route from amazon, and up until now I've been able to pull off what I need by searching the web and clipping parts of other folk's examples.
The code below does exactly what I need, but it takes a LOOOONG time...
Im searching for the two latest .csv's in a folder containing thousands of files, they have "-L" and "-R" in the file name, repectively. The file is copied into the destination folder, renamed, opened, closed, and deleted.
All of this is to refresh the data in the master sheet from which the sub is ran on a daily basis. Other than the -L and -R, the file names vary greatly depending on the person generating the .csv files in the source folder.
I would greatly appreciate any advice on how I could improve my process or maybe point me in the right direction when I begin studying a little deeper.
Code:
Sub LatestFileWithName()
'Jim Cone - San Francisco, USA - July, 2006
'Displays the latest file name in the strPath folder,
' if the file name contains a specified word.
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim strPath As String
Dim strName As String
Dim varDate As Variant
Dim strFind As String
Dim sTarget As String
' Specify the folder...
strPath = "\\FILPRTSRV01\Data\Shared\Quality\CMM Data\MSQC CMM Data\59B Fuel Pump\Revo 3\res\"
' Specify the word in the file name...
strFind = "-L"
sTarget = "C:\Users\KennyS\Desktop\59B FP\"
' Use Microsoft Scripting runtime.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)
' Check date on each file in folder.
For Each objFile In objFolder.Files
If InStr(1, objFile.Name, strFind, vbTextCompare) Then
If objFile.DateLastModified > varDate Then
strName = objFile.Name
varDate = objFile.DateLastModified
End If
End If
Next 'objFile
FileCopy strPath & strName, sTarget & strName
strFind = "-R"
' Use Microsoft Scripting runtime.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)
' Check date on each file in folder.
For Each objFile In objFolder.Files
If InStr(1, objFile.Name, strFind, vbTextCompare) Then
If objFile.DateLastModified > varDate Then
strName = objFile.Name
varDate = objFile.DateLastModified
End If
End If
Next 'objFile
FileCopy strPath & strName, sTarget & strName
'SET VARIABLES TO RENAME DATA FILES
sSearch = "C:\Users\KennyS\Desktop\59B FP\"
sTarget = "C:\Users\KennyS\Desktop\59B FP\"
sKey = "-L"
sFilename = Dir(sSearch & "*" & sKey & "*" & ".CSV")
FileCopy sSearch & sFilename, sTarget & "FPL.CSV"
sFilename = Dir()
sKey = "-R"
sFilename = Dir(sSearch & "*" & sKey & "*" & ".CSV")
FileCopy sSearch & sFilename, sTarget & "FPR.CSV"
sFilename = Dir()
'OPEN DATA FILES
Workbooks.Open ThisWorkbook.path & "\" & "FPL.CSV"
Workbooks.Open ThisWorkbook.path & "\" & "FPR.CSV"
ThisWorkbook.Activate
'CLOSE DATA FILES
Dim Wb As Workbook
For Each Wb In Application.Workbooks
If StrComp(Right(Wb.Name, 4), ".CSV", vbTextCompare) = 0 Then
Wb.Close
End If
Next Wb
'DELETE CSV FILES
'Kill "C:\Users\KennyS\Desktop\59B FP\*.csv"
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
End Sub