Copy most recent file and move to another folder

Ananthak275

Board Regular
Joined
Aug 22, 2020
Messages
128
Office Version
  1. 2013
Platform
  1. Windows
  2. MacOS
Trying to create a VBA that copies the most recent file in folder based on name. The folder would have files in this format (seen below)..


In this example; I would want it to copy "AFILENAMENAMES_2022-10-20 - (Published On 2022-10-20 10-09-16)" and move to another folder.

AFILENAMENAMES_2022-10-20 - (Published On 2022-10-20 10-09-16)
AFILENAMENAMES_2022-10-19 - (Published On 2022-10-19 09-04-55)
AFILENAMENAMES_2022-10-18 - (Published On 2022-10-18 09-16-22)
AFILENAMENAMES_2022-10-17 - (Published On 2022-10-17 09-17-49)
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi Ananthak275,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim strPathFrom As String, strPathTo As String, strFile As String, strFileToMove As String
    Dim dteFileDate As Date
   
    Application.ScreenUpdating = False
   
    'Paths to find the latest file from path 'strPathFrom' and then to copy that file to path 'strPathTo'. Obviously change to suit.
    strPathFrom = "C:\VBA Examples\Copy Latest File"
    strPathFrom = IIf(Right(strPathFrom, 1) <> "\", strPathFrom & "\", strPathFrom)
    strPathTo = "C:\VBA Examples\Copy Latest File\Move To Here\"
    strPathTo = IIf(Right(strPathTo, 1) <> "\", strPathTo & "\", strPathTo)
   
    strFile = Dir(strPathFrom & "*.xls*") 'For Excel files only

    Do While Len(strFile) > 0
        If CDate(Mid(strFile, 16, 10)) > dteFileDate Then
            strFileToMove = strFile
            dteFileDate = CDate(Mid(strFile, 16, 10))
        End If
    strFile = Dir
    Loop
   
    If Len(strFileToMove) > 0 Then
        FileCopy strPathFrom & strFileToMove, strPathTo & strFileToMove
        MsgBox strFileToMove & " has now been copied.", vbInformation
    Else
        MsgBox "The code not find a file to copy.", vbExclamation
    End If
   
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Solution

Forum statistics

Threads
1,215,545
Messages
6,125,448
Members
449,227
Latest member
Gina V

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