Skip to content

Excel VBA: Archive Workbooks Based on Date Created

    This video features the code you could use to move workbooks to another folder, but only if the workbook was created before a specified date. In our scenario, workbooks created before Jan 2019 are archived, any other workbooks remain in the original folder.

    Here’s the code featured in the video

    Sub ArchiveFiles()
    
    Dim fso As Scripting.FileSystemObject
    Set fso = New Scripting.FileSystemObject
    
    Dim srcfol As Scripting.Folder
    Set srcfol = fso.GetFolder("C:\Users\Blue Pecan\Desktop\Statements\")
    
    Dim destfol As Scripting.Folder
    Set destfol = fso.GetFolder("C:\Users\Blue Pecan\Desktop\Archive\")
    
    Dim fle As Scripting.File
    
    Dim flecreated As Date
    
    For Each fle In srcfol.Files
        flecreated = fle.DateCreated
    
        If flecreated < "01/01/2019" Then _
        fso.MoveFile fle.Path, destfol.Path & "\" & fle.Name
    Next fle
    
    End Sub