Last Modified File Name into Specific Cell

Cagey93

New Member
Joined
Apr 17, 2019
Messages
34
I have a project going on at the moment whereby I need to go into multiple folders and pick up some files to convert to PDF. That part I am fine with but to get the file names to start I want a Macro to run and get the name of the latest modified file and put that name into a specific cell.

An example would be go to "C:\Documents" and look at the last modified date of each file and place the latest one into cell "D3".

I have searched high and low but cant seem to get anything that does this.

Is there anyone who could help me out?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this.
The result will be in cells D3 and E3

Code:
Sub Last_Modified_File()
    Dim wPath As String, wName As String
    Dim wFile As Object, wFso As Object, wMax As Variant
    
    wPath = "C:\[COLOR=#333333]Documents[/COLOR]\"
    Set wFso = CreateObject("Scripting.FileSystemObject")
    For Each wFile In wFso.GetFolder(wPath).files
        If wFile.DateLastModified > wMax Then
            wMax = wFile.DateLastModified
            wName = wFile.Name
        End If
    Next
    Range("D3").Value = wName
    Range("E3").Value = wMax
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,037
Members
448,543
Latest member
MartinLarkin

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