Schedule A Macro To Run With Scheduled Tasks

RoyinUK2000

Board Regular
Joined
Mar 26, 2002
Messages
79
I have a worksheet that runs a query from an external data program. I have made a macro that refreshes the worksheet and this works ok. Is there a way I can get Scheduled tasks to automatically update this workbook eg overnight. I do not want to leave the workbook open either. Can task scheduler open the workbook, refresh the data, then close the workbook again.


Please help

Thanks

Roy
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Yes,

Add the excel file to the task scheduler.

Add an Autoopen macro to your excel book

Add in lines to save the book and then close the application into the macro.

The quit code is as follows.

application.quit

To add the file to scheduled tasks select add a task...Then when the program screen pops up click browse and just navigate to your Excel file...you dont have to start Excel your PC will do that automatically.
 
Upvote 0
This should work for you...
Place in your Workbook Class Code Module<pre>Private Sub Workbook_Open()
TheNameOfYourCurrentProcedureHere
Me.Close True 'save the changes and close the workbook
End Sub

If you would like to limit this procedure to only run when
opened by the scheduler, then test the time. Fo ex. Say you
are running this procedure Sun - Fri at 0330 AM. Use code similiar
to the following...

Private Sub Workbook_Open()
If Hour(Now) = 3 And WeekDay(Now, vbSunday)< 7 Then
TheNameOfYourCurrentProcedureHere
Me.Close True 'save the changes and close the workbook
End If
End Sub</pre>Tom

We're not a vending machine or fast food! :smile:
_________________
Using Office 8, 9, & 10 on Windows Millenium
This message was edited by TsTom on 2003-01-28 07:28
 
Upvote 0
Is there a way to point task scheduler to a specific macro rather than have the sheet refreshing every time it is opened by using an auto-open macro that refreshes.


Please help.

Roy.
 
Upvote 0
Cleaner code here which end Excel if this is the only workbook open at the time...<pre>Private Sub Workbook_Open()
If Hour(Now) = 3 And WeekDay(Now, vbSunday) < 7 Then
' TheNameOfYourCurrentProcedureHere
If Workbooks.Count = 1 Then
Me.Save
Application.Quit
Else
Me.Close True 'save the changes and close the workbook
End If
End If
End Sub

</pre>om
 
Upvote 0
On 2003-01-28 07:27, RoyinUK2000 wrote:
Is there a way to point task scheduler to a specific macro rather than have the sheet refreshing every time it is opened by using an auto-open macro that refreshes.


Please help.

Roy.

Not absolutely sure what you mean...you can not run a specific macro from one file.

However, you could open one file that contains an autoopen macro which then calls other files and runs macros in those...

Is this the sort of thing you mean...
 
Upvote 0
He wants the sheet to automatically run code ONLY when it is opened byt the task scheduler. One method was listed. There might be other alternatives???
 
Upvote 0
While the above suggestions are quite good, below is a slight variation which does NOT require ensuring the Scheduled Task run at a specific time (or that the Macro and the Scheduled Task be in "agreement" as to WHEN the automation happens):

Private Declare Function GetActiveWindow Lib "user32.dll" () As Long

Private Sub Workbook_Open()
'
' Macro runs when workbook opens
' Runs when Spreadsheet (worksheet) is OPENED. Allows running Excel from Task Scheduler (Update/Save/Quit).
'
Call Application.OnTime(VBA.Now, "ThisWorkbook.TestInteractive")
End Sub

Private Sub TestInteractive()
If GetActiveWindow() = 0 Then
' We're running via the Task Scheduler: Update the Spreadsheet/Graph
If Workbooks.Count = 1 Then
' YourMacroNameHere
Me.Save
Application.Quit
Else
Me.Close True 'save the changes and close the workbook
End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,218
Members
453,024
Latest member
Wingit77

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