move file out of folder based on earliest date modified

rjmdc

Well-known Member
Joined
Apr 29, 2020
Messages
672
Office Version
  1. 365
Platform
  1. Windows
i need vba please to cut file out of original folder and pasteinto another folder based on earliest modified date
the folder will have 2 files at any given time and i need to remove the earlier one to leave the latter one for processing
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try this, changing the folders as required.
VBA Code:
Public Sub Move_Earliest_File()

    Dim fromFolder As String, toFolder As String
    Dim file1 As String, file2 As String
    
    fromFolder = "C:\path\to\folder1\"   'CHANGE THIS
    toFolder = "C:\path\to\folder2\"     'CHANGE THIS

    file1 = Dir(fromFolder & "*.*")
    file2 = Dir()
    If FileDateTime(fromFolder & file1) < FileDateTime(fromFolder & file2) Then
        Name fromFolder & file1 As toFolder & file1
    Else
        Name fromFolder & file2 As toFolder & file2
    End If
    
End Sub
 
Upvote 0
please explain the if and else
i want to leave the latter one in the current file and move the earlier one out
 
Upvote 0
The If statement looks at the modified date of file1 and file2 and moves file1 if it is earlier than file2, otherwise it moves file2.
 
Upvote 0
sorry it doesnt move the folder out of folder 1 into folder 2
 
Upvote 0
thanks i had an error
this works perfectly
i really appreciate
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,509
Members
448,967
Latest member
screechyboy79

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