Automated Excel .csv import on a schedule?

%3CLINGO-SUB%20id%3D%22lingo-sub-2898295%22%20slang%3D%22en-US%22%3EAutomated%20Excel%20.csv%20import%20on%20a%20schedule%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2898295%22%20slang%3D%22en-US%22%3E%3CP%3EDoes%20Excel%20have%20the%20ability%20to%20automatically%20import%20a%20.csv%20file%20on%20a%20schedule%3F%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20.csv%20file%20import%20that%20needs%20to%20run%20every%20morning.%26nbsp%3B%20%26nbsp%3BI%20know%20that%20there%20is%20a%20way%20to%20automate%20the%20import%20task%2C%20but%20I%20want%20to%20be%20able%20to%20have%20the%20import%20automatically%20run%20on%20a%20schedule.%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2898295%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor

Does Excel have the ability to automatically import a .csv file on a schedule?  

 

I have a .csv file import that needs to run every morning.   I know that there is a way to automate the import task, but I want to be able to have the import automatically run on a schedule.   

1 Reply

@VailA45987 

One option is to keep the workbook open 24/7.

In a standard module:

Public dtmNext As Date

Sub ImportCSV()
    ' Code to import CSV goes here
    ' ...
    ' Next run at 9 AM tomorrow
    dtmNext = Date + 1 + TimeSerial(9, 0, 0)
    Application.OnTime EarliestTime:=dtmNext, Procedure:="ImportCSV"
End Sub

In the ThisWorkbook module:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    On Error Resume Next
    ' Cancel scheduled run
    Application.OnTime EarliestTime:=dtmNext, Procedure:="ImportCSV", Schedule:=False
End Sub

Private Sub Workbook_Open()
    ' Next run tomorrow at 9 AM
    dtmNext = Date + 1 + TimeSerial(9, 0, 0)
    ' Or if it's not 9 AM yet, today
    If Hour(Now) < 9 Then
        dtmNext = dtmNext - 1
    End If
    Application.OnTime EarliestTime:=dtmNext, Procedure:="ImportCSV"
End Sub