Hi there,
I currently use the following VBA code to find the most recent file within a folder, open it and copy the data to another sheet. I'm looking for a way to close it again afterwards.
Private Sub Workbook_Open()
Application.ScreenUpdating = False
Sheets("Price List").Visible = True
Dim CSV_file As Object
Dim CSV_folder As String
Dim file_ext As String
Dim FSO As Object
Dim most_recent As Object
file_ext = "csv"
CSV_folder = "DIRECTORY/FILENAME"
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each CSV_file In FSO.GetFolder(CSV_folder).Files
If FSO.GetExtensionName(CSV_file) = file_ext Then
If most_recent Is Nothing Then Set most_recent = CSV_file
If CSV_file.DateLastModified > most_recent.DateLastModified Then
Set most_recent = CSV_file
End If
End If
Next CSV_file
If Not most_recent Is Nothing Then Workbooks.Open most_recent
Set FSO = Nothing
Cells.Select
Selection.Copy
Windows("ORDERING.xls").Activate
Sheets("Price List").Select
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("Price List").Visible = False
Application.ScreenUpdating = True
Sheets("Order").Select
MsgBox "Please close the spreadsheet F4122_catalogue"
End Sub
Currently I am relying on the user by presenting them with a prompt box, I would like to avoid this altogether. Does anyone have any suggesstions?
in addition I'm also wondering if it would be possible to enter the file name (actually part of the filename) returned with this code into a specific cell within the worksheet?
Many thanks in advance.
Bookmarks