简体   繁体   中英

Launch a macro when Excel is off

Tl:dr Looking to see if there is any way to launch an Excel VBA macro when Excel is not open.

Long version: I have a macro that fetches data from a specific set of websites, and updates the spreadsheets accordingly. The only problem with this is that the data updates at inconvenient times, namely Monday mornings at 05.00 (GMT), and Thursday mornings at 01.15 (GMT).

What I need is a way of Excel self launching, collecting the data, update the spreadsheet, and then shutting down. It's this possible?

If I were you, I would it this way:

At First I install a third-party program, with I open the XLSM file, when it is needed.

Once Excel has opened your spread-sheet you can create a Sub Workbook_Open() routine to automatically run your macro. Your macro can close the spread-sheet once it has completed, and exit Excel.

Easier still - no need for VB scripting.

Run WINDOWS TASK SCHEDULER, set up a job using Excel as the application.

在此处输入图片说明

You'll have to search for Excel.exe - because of my version, I found it here:

"C:\\Program Files (x86)\\Microsoft Office\\Office15\\EXCEL.EXE"

Next supply the Argument as the FULL PATH to the macro-embedded Excel file. For my testing, I invoked a file on my desktop:

C:\\Users\\br12260\\Desktop\\TimeSeries.xls

Then just be sure that your workbook has an Workbook_Open procedure in ThisWorkbook module to trigger your code:

在此处输入图片说明

The best way is to write a VB.net script to do that.

The easiest way is to left the PC turned on during the night and follow this simple tutorial:

Public dTime As Date
Dim lNum As Long

Sub RunOnTime()
    dTime = Now + TimeSerial(0, 0, 10)
    Application.OnTime dTime, "RunOnTime"

    lNum = lNum + 1
    If lNum = 3 Then
        Run "CancelOnTime"
    Else
        MsgBox lNum
    End If

End Sub

Sub CancelOnTime()
    Application.OnTime dTime, "RunOnTime", , False
End Sub

Another way is use the Windows Scheduler, as written in this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM