Refresh All vba from another Workbook using Task Scheduler

Plukey

Board Regular
Joined
Apr 19, 2019
Messages
138
Office Version
  1. 2016
Platform
  1. Windows
I'm currently using Task Scheduler to open a workbook. That workbook has a Workbook Open vba that opens the Target Workbook and refresh all. I don't use Task Scheduler to open the target workbook because its being opened through out the day for viewing.

It works, I'm just wondering if anyone uses a different method that's better than the one I'm using.
VBA Code:
Sub Workbook_Open()
With Workbooks.Open("C:Path\.xlsm")
        .RefreshAll
        Application.Wait Now + TimeValue("00:00:02")
        .Save
        .Close True
    End With
Workbooks("Workbook.xlsm").Save
MsgBox "Update complete"
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
It wouldn't be very different from what you're currently doing, but you could always convert the above into a VBScript file and have Task Scheduler execute it direct. VBScripts are effectively text files that contain a form of Visual Basic that can be executed like any other executable. So if you were to type
VBA Code:
MsgBox "Hello"
into a text file and save that file to have the extension VBS, you could run it simply by double clicking on the file in File Explorer.

I haven't tested it, but the following in a VBScript file should reproduce what you have above:

VBA Code:
Dim XLApp, WB, Filename
Set XLApp = CreateObject("Excel.Application")
Filename = "C:\PATHTOYOURFILE\File.xlsm"
Set WB = XlApp.Workbooks.Open(Filename)
WB.RefreshAll
XLApp.Wait Now + TimeValue("00:00:02")
WB.Save
WB.Close True
XLApp.Quit
MsgBox "Update complete"

What it doesn't do is save Workbooks.xlsm, because I assume that's the workbook you've opened in order to run the code?
 
Upvote 0
Solution
It wouldn't be very different from what you're currently doing, but you could always convert the above into a VBScript file and have Task Scheduler execute it direct. VBScripts are effectively text files that contain a form of Visual Basic that can be executed like any other executable. So if you were to type
VBA Code:
MsgBox "Hello"
into a text file and save that file to have the extension VBS, you could run it simply by double clicking on the file in File Explorer.

I haven't tested it, but the following in a VBScript file should reproduce what you have above:

VBA Code:
Dim XLApp, WB, Filename
Set XLApp = CreateObject("Excel.Application")
Filename = "C:\PATHTOYOURFILE\File.xlsm"
Set WB = XlApp.Workbooks.Open(Filename)
WB.RefreshAll
XLApp.Wait Now + TimeValue("00:00:02")
WB.Save
WB.Close True
XLApp.Quit
MsgBox "Update complete"

What it doesn't do is save Workbooks.xlsm, because I assume that's the workbook you've opened in order to run the code?
Nice! I was just reading up on VBScript, I'll give it go. Thanks for the idea!
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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