Hi guys! Hope you are all well!
This is the first time I try this forum, so please do let me know if I didn't do something correctly. Also, I am a self taught VBA noob, so it would be greatly appreciated if you can please dumb it down as much as possible in the explanation/code. Thanks heaps for your help in advance! So here we go....
Each day, the system will create 3 files in the morning, afternoon and night into the folder. The file will start with the same file name with today's date but just end with different suffix. To make the matter simpler, let's assume the suffix here is randomly generated. Then the system will generate these files through out the month.
e.g.
File_02_May_3214
File_02_May_5425
File_02_May_6534
File_03_May_5424
File_03_May_3224
File_03_May_1343
File_04_May_7654
File_04_May_4324
File_04_May_9876
At the month end (after daily files for the entire month are generated), my goal here is to open each day's file which has the latest modified date in the folder and copy something from each of the file and paste on to a worksheet.
So for example, for the below files
e.g. (files in the folder)
File_02_May_3214 (<=modified date 9:00am)
File_02_May_5425 (<=modified date 13:00pm)
File_02_May_6534 (<=modified date 5:00pm)
File_03_May_5424 (<=modified date 9:00am)
File_03_May_3224 (<=modified date 13:00pm)
File_03_May_1343 (<=modified date 5:00pm)
File_04_May_7654 (<=modified date 9:00am)
File_04_May_4324 (<=modified date 13:00pm)
File_04_May_9876(<=modified date 5:00pm)
I will want to open File_02_May_6534, then copy some data and paste them into a worksheet, then move onto File_03_May_1343 and File_04_May_9876 to do the same.
My solution to do the above action via VBA is to firstly create a daily file name list with wildcard naming convention in Excel (like the below).
e.g. (file name list)
File_02_May_*
File_03_May_*
File_04_May_*
I will loop through this daily file name list and in each loop, I will also find the daily file with the latest modified date via another loop and once found, i will open and copy some data out of those files. However, I have some issue doing it via my below code (red text section) unfortunately.
As you can see, there are two loops with the below code. One is to "loop through the daily file name list" and another is to "loop to find the daily file with the latest modified date in the folder". Once the file is located, it will open it up and copy/paste some data out of it. However, for some reason, once it finds the latest file for the day, instead of going to open the file, it goes to find the next latest modified file for the next file on the daily file name list.
So using the above example again, once my code finds File_02_May_6534, instead of opening it, it goes onto searching for the next daily file (File_03_May_*) with the latest modified date in the loop. Can someone please help to provide some insight for this issue here? Or is there a better or simpler way to tackle this issue? Thank you so much!
This is the first time I try this forum, so please do let me know if I didn't do something correctly. Also, I am a self taught VBA noob, so it would be greatly appreciated if you can please dumb it down as much as possible in the explanation/code. Thanks heaps for your help in advance! So here we go....
Each day, the system will create 3 files in the morning, afternoon and night into the folder. The file will start with the same file name with today's date but just end with different suffix. To make the matter simpler, let's assume the suffix here is randomly generated. Then the system will generate these files through out the month.
e.g.
File_02_May_3214
File_02_May_5425
File_02_May_6534
File_03_May_5424
File_03_May_3224
File_03_May_1343
File_04_May_7654
File_04_May_4324
File_04_May_9876
At the month end (after daily files for the entire month are generated), my goal here is to open each day's file which has the latest modified date in the folder and copy something from each of the file and paste on to a worksheet.
So for example, for the below files
e.g. (files in the folder)
File_02_May_3214 (<=modified date 9:00am)
File_02_May_5425 (<=modified date 13:00pm)
File_02_May_6534 (<=modified date 5:00pm)
File_03_May_5424 (<=modified date 9:00am)
File_03_May_3224 (<=modified date 13:00pm)
File_03_May_1343 (<=modified date 5:00pm)
File_04_May_7654 (<=modified date 9:00am)
File_04_May_4324 (<=modified date 13:00pm)
File_04_May_9876(<=modified date 5:00pm)
I will want to open File_02_May_6534, then copy some data and paste them into a worksheet, then move onto File_03_May_1343 and File_04_May_9876 to do the same.
My solution to do the above action via VBA is to firstly create a daily file name list with wildcard naming convention in Excel (like the below).
e.g. (file name list)
File_02_May_*
File_03_May_*
File_04_May_*
I will loop through this daily file name list and in each loop, I will also find the daily file with the latest modified date via another loop and once found, i will open and copy some data out of those files. However, I have some issue doing it via my below code (red text section) unfortunately.
As you can see, there are two loops with the below code. One is to "loop through the daily file name list" and another is to "loop to find the daily file with the latest modified date in the folder". Once the file is located, it will open it up and copy/paste some data out of it. However, for some reason, once it finds the latest file for the day, instead of going to open the file, it goes to find the next latest modified file for the next file on the daily file name list.
So using the above example again, once my code finds File_02_May_6534, instead of opening it, it goes onto searching for the next daily file (File_03_May_*) with the latest modified date in the loop. Can someone please help to provide some insight for this issue here? Or is there a better or simpler way to tackle this issue? Thank you so much!
Rich (BB code):
Sub Import_file()
Dim myDir As String
Dim File As Range
Dim FileName_widecarded As String
Dim msg As String
Dim LMD As Date
Dim Latestdate As Date
Dim Latestfile As String
myDir = Range("File_directory")
For Each File In Range("File_range")
FileName_widecarded = Dir(myDir & File, vbNormal)
If FileName_widecarded <> "" Then
Do While Len(FileName_widecarded) > 0
LMD = FileDateTime(myDir & FileName_widecarded)
If LMD > Latestdate Then
Latestfile = FileName_widecarded
Latestdate = LMD
End If
FileName_widecarded = Dir
Loop
Workbooks.Open myDir & Latestfile
ActiveSheet.Range("A1:J5000").Copy Destination:=ThisWorkbook.Worksheets("Data raw").Range("A1:B20")
ActiveWorkbook.Close False
Else
msg = msg & vbLf & File.Value
End If
Next
Last edited by a moderator: