vba code needed

peter007

New Member
Joined
Jan 23, 2014
Messages
35
There are files that get saved in a drive every 15 or 20 or 30 mins randomly(no fixed interval). I need a VBA code that would automatically get executed every hour and could copy the files that got saved for the previous one hour and these files should get saved in a target folder. Please help me with a vba code
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
.
.

Please try the following macro.

Before running, you'll need to read over the code and make some changes (in the places I've indicated using comments).

You'll also need to leave this workbook open in the background in order for the macro to call itself again after an hour has passed.

Code:
Option Explicit

'Module-level variable
Dim EndTime As Variant

Sub CopyNewFiles()

    'Variables for source and
    'destination files...
    Dim SFile As String
    Dim SPath As String
    Dim DPath As String
    
    'Set end time for initial run
    If IsEmpty(EndTime) Then
        EndTime = CDate(Now - TimeValue("01:00"))
    End If
    
    'Set source folder
    'Change accordingly...
    Const SFold As String = "C:\Users\gpeacock\Downloads\py3e_source\chapter03"
    
    'Get first source filename
    SFile = Dir(SFold & Application.PathSeparator & "*")
    
    'If no source files, wait an hour
    If SFile = vbNullString Then GoTo Wait
    
    'Set destination folder
    'Change accordingly...
    Const DFold As String = "C:\Users\gpeacock\Desktop\destination_folder"
    
    'Loop through source files and
    'copy files created during last hour...
    
    Do While SFile <> vbNullString
    
        SPath = SFold & Application.PathSeparator & SFile
        DPath = DFold & Application.PathSeparator & SFile
        
        If FileDateTime(SPath) >= EndTime Then
            FileCopy Source:=SPath, Destination:=DPath
        End If
        
        SFile = Dir
        
    Loop
    
    'Wait for one hour
    'before running again...
Wait:
        
    'set end time
    EndTime = CDate(Now)
        
    Application.OnTime _
        EarliestTime:=Now + TimeValue("01:00"), _
        Procedure:="CopyNewFiles"

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,742
Members
448,989
Latest member
mariah3

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