Import/retrieve newest date from file name.

Totalnovice

New Member
Joined
Mar 30, 2022
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
Hello,
My problem is probably very simple for you experienced users. So I have file directory, where at regular basis new html-file is added (picrure below). The files have the date in the file name and also as modified date.

What I'd like to do is to have VBA that retrieves the date of the newest file to excel.

Being a novice with VBA I've just googled and tried to find the solution but haven't managed to find the right one yet. So far I've gotten to point where I can get file names imported to excel with VBA (working somewhat alright). Hopefully I can get some help here. Thanks

File directory.PNG

-Totalnovice
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Assuming there are 3 files in folder: "E:\TestFolder"
Below code will paste the newest date in A1, and the file name in A2:
VBA Code:
Option Explicit
Sub GetNewestDate()
Dim maxD As Long, xFolder As Object, xF As Object, xFile As Object, xDate As String, mFile As String
Set xF = CreateObject("Scripting.FileSystemObject")
Set xFolder = xF.getFolder("E:\TestFolder")
    For Each xFile In xFolder.Files
        xDate = Mid(xFile.Name, 6, 8) + 0
        If xDate > maxD Then
            maxD = xDate
            mFile = xFile.Name
        End If
    Next
Range("A1").Value = maxD
Range("A2").Value = mFile
End Sub

Capture.JPG
 
Upvote 0
Solution
Thank you so much. This did the trick almost perfectly. Only minor thing is that it returns the date as yyyymmdd. In the cells it can be changed to dd.mm.yyyy ie. by using DATE and VALUE, but how should this be implemeted in VBA so it would do it automatically?
 
Upvote 0
Try:
VBA Code:
Range("A1").Value = DateSerial(Left(maxD, 4), Mid(maxD, 5, 2), Right(maxD, 2))
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top