 |
| Excel VBA Discuss using VBA for Excel programming. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 27th, 2008, 11:47 AM
|
|
Registered User
|
|
Join Date: Oct 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Schedule a VBA macro to run at a specific time
Hello,
I need to schedule my PC to run an Excel macro automatically at a specified time, for exmaple, 6 PM every day. I have no idea about how to do that. Any help would be greatly appreciated.
Peter
|
|

October 27th, 2008, 07:25 PM
|
|
Authorized User
|
|
Join Date: Nov 2007
Posts: 48
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
Peter,
http://www.ozgrid.com/Excel/run-macro-on-time.htm
There are many times when it would be great to have any macro run at a predetermined time or run it at specified intervals. Fortunately Excel has made this a relatively simple task, when you know how.
Application.OnTime
This Method is what we can use to achieve the automatically running of Excel Macros. Let's suppose we have a macro that we wish to Run each day at 18:00 (6:00 PM). The first problem will be how to kick-off the OnTime Method. This we can do via the Workbook Open Event. The fastest way to get to the Private Module of the Workbook Object (ThisWorkbook) is to right click on the Excel icon next to "File" and select "View Code"
Code:
Private Sub Workbook_Open()
Application.OnTime TimeValue("18:00:00"), "MyMacro"
End Sub
Have a great day,
Stan
[/code]
stanleydgromjr
Windows Vista Business and Excel 2003, 2007.
|
|

October 28th, 2008, 04:54 PM
|
|
Registered User
|
|
Join Date: Oct 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Stan,
Thank you very much for your reply. But I think the problem still remains: How to schedule the procedure of "Workbook_Open" to start automatically at a predetermined time, especially when the Excel is not open.
Have a good day,
Peter
|
|

October 29th, 2008, 02:10 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
|
|
Hi Peter
I don't think you can fire the Open event from outside. Either you need to open the Workbook for the event to be fired
or
Use Application.Run to call the Macro in the workbook
Code:
Sub RunAMacro()
Application.Run "'C:\temp\MAcWB.xls'!MyMacro"
End Sub
Cheers
Shasur
http://www.dotnetdud.blogspot.com
VBA Tips & Tricks ( http://www.vbadud.blogspot.com)
|
|

October 29th, 2008, 11:50 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 57
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
|
|

October 29th, 2008, 08:09 PM
|
|
Registered User
|
|
Join Date: Oct 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks,
Peter
|
|

April 4th, 2013, 03:49 AM
|
|
Registered User
|
|
Join Date: Apr 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Suggest
Hi,
I used this code to automate my macro
Private Sub Workbook_Open()
Application.OnTime TimeValue("08:58:00"), "RUN"
Application.OnTime TimeValue("09:00:00"), "RUN"
Application.OnTime TimeValue("10:00:00"), "RUN"
Application.OnTime TimeValue("11:50:00"), "RUN"
Application.OnTime TimeValue("12:00:00"), "RUN"
Application.OnTime TimeValue("13:00:00"), "RUN"
Application.OnTime TimeValue("13:10:00"), "RUN"
Application.OnTime TimeValue("14:00:00"), "RUN"
Application.OnTime TimeValue("15:00:00"), "RUN"
Application.OnTime TimeValue("15:33:00"), "RUN"
Application.OnTime TimeValue("16:00:00"), "RUN"
Application.OnTime TimeValue("16:30:00"), "RUN"
Application.OnTime TimeValue("17:00:00"), "RUN"
Application.OnTime TimeValue("17:30:00"), "RUN"
Application.OnTime TimeValue("18:00:00"), "RUN"
Application.OnTime TimeValue("19:00:00"), "RUN"
Application.OnTime TimeValue("20:00:00"), "RUN"
End Sub
Sub RunAMacro()
Application.RUN "path'!RUN"
End Sub
But the problem is...If my cursor is placed on other excel sheet at above mentioned time then macro executed in that sheet not in 'SGX Macro Annuals'.
Could any one suggest the currect code.
Thanks
Last edited by psukanya; April 4th, 2013 at 04:28 AM..
|
|

October 26th, 2015, 01:36 AM
|
|
Registered User
|
|
Join Date: Oct 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I tried this but it is not running can you share excel file.
|
|

October 31st, 2015, 08:41 AM
|
|
Authorized User
|
|
Join Date: Oct 2015
Posts: 48
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Hi,
I think you need to use Windows task scheduler to run the workbook at a specified interval.
Here is the XML of an example task.
<?xml version="1.0" encoding="UTF-16" ?>
- <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo />
<Triggers />
- <Principals>
- <Principal id="Author">
<UserId>MyName-PC\MyAdminName</UserId>
<LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
- <Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
- <IdleSettings>
<Duration>PT10M</Duration>
<WaitTimeout>PT1H</WaitTimeout>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>5</Priority>
</Settings>
- <Actions Context="Author">
- <Exec>
<Command>C:\Program Files (x86)\IObit\IObit Uninstaller\IObitUninstaler.exe</Command>
<Arguments>/UninstallExplorer</Arguments>
</Exec>
</Actions>
</Task>
__________________
Nostalgia 4 Infinity
Last edited by Zakalwe; October 31st, 2015 at 08:55 AM..
|
|
 |