how to automatically run a macro at a given hour - vba

I'm wondering if anyone here has experience with setting up a macro to run automatically at a given time... lets say 5AM.
There are some posts on mr excel but most of what I find is simply OnTime "x time" based code, and I'm unsure how this executes.
Do you have to open the sheet, run the macro and then expect it to run at the given time? Once it is open, will it run every day at that time or can you run it whenever?

You could
1] have Windows task scheduler open your workbook. Please see: http://windows.microsoft.com/en-us/windows7/schedule-a-task
2] and then create a function in your macro that launches your code when the book is opened. See http://www.mrexcel.com/forum/excel-questions/6122-run-macro-open.html for more info.

This seems to be what you want:
How can I Run Excel Files as a task or job?
Use the Windows Scheduler to schedule a task to open up the Excel file. You should then be able to use the Workbook_Open Event to do what you need with the file.

Related

Prevent >1 person from running VBA macro at a time (shared wkb)

What I'm aiming at is getting a list of all currently running scripts in order to check if other users aren't running the same VBA macro at the same time (and if yes, then stop the code etc., similar to what the OP of the below question wanted). This would be for a shared workbook (I learned it's not designed for this type of work, but I need to try it).
https://stackoverflow.com/a/36116091/5947935
I've been trying to make the code in the above answer work in VBA, but it seems it's a vbs thing and I would like to avoid that.
I'm not an expert to say the least, so I'm having trouble understanding how to get this to work in Excel VBA. I don't even know if it's possible at all.
I've found this as well: VBA Getting program names and task ID of running processes and it works fine but it only lists the running processes.
I've no idea however how to merge the two... or even if the WMI is the correct way to go.
I'd appreciate any sort of help.
I used to create a "locking file" which was just an empty text file with the name of the workbook followed by the username and an extension of .LCK
First thing my code did on auto open was look for a locking file then report back to the user which user had it open then cancel the open.
If it didn't find a locking file then it created one and proceeded as normal. If it found one but it was the same username (ie that user had it crash on them) it proceeded with the open.
The last thing the code did was delete the file.
No codes here and theory not tested yet, but the idea of preventing a different user executing a Macro on a shared workbook requires some thinking.
I would create a hidden worksheet, and use one of the cells to store the Environ("USERNAME") when the macro is first started - to indicate who has it running, then clear it when complete, first-in-first-out.
Lets say named range MUser (macro user) is range A1 in that hidden worksheet
When the macro runs, it will first check if MUser is empty, if so then change it's value to Environ("USERNAME") and Save the file before next step (here I am not certain the value is updated on others session).
If MUser is not empty, either abort or retry in a few second.
When macro completes, MUser will be ClearContents, and save the File to free up the workbook for macro.
Idea is here but please test. Post your own code for us to troubleshoot. You may also use Workbook events to "lock" the macro execution this way. Or even use this hidden sheet to make a log record for debug. Also some fail-safe needs to be implemented (such as a time stamp at macro start and override the lock after some minutes).

Continuous Loop/Repeat causes Excel to Crash (after hours/days)

I have some VBA code that runs every 15 minutes continually. This code runs fine at first but after a period of time (which varies from around 4 hours to 5 days) Excel crashes.
It is not a VBA crash (with debug option) but it is Excel freezing and saying the application needs to be restarted.
Am I trying to use Excel in a way it was not designed for? Should I be clearing some memory/cache to avoid this?
I believe it is due to a fail of the OnTime method of the application, I had your same problem some time ago with an Excel tool which was scraping data from the web every 5 minutes. Sometimes it just crashed with no specific log or error/warning. Here is the workaround I have implemented:
1) In the Workbook_Open event I have put :
myMacro 'the call to my procedure when the workbook is opened
ThisWorkbook.Save 'I was saving the results, I don't know if you need this
ThisWorkbook.Close 'I was closing my Workbook
2) In the Windows system, I was using a tool (installed usually with the system) called Task Scheduler, whose executable lies into the system folder (C:\Windows\System32\taskschd.msc) and it's really intuitive and easy-to-use. I have scheduled a task every 5 minutes which consisted on opening the workbook (once this was done, the open-workbook-event macro was triggered so my procedure was called, and after its execution the Workbook was closed and saved with the two lines of code I have posted you above).
Even if this is just my opinion (that I cannot prove with technical evidences), I don't trust too much the Application.OnTime in the long-term; it works fine for a little task every 10 seconds for a few minutes, but when the macro should be running regularly and you want to avoid seeing that, once every 5 hours, the Excel process crashes... then I would suggest to let the scheduling task to the Microsot tool rather than to the Excel application method.
MY EXPERIENCE
- my Excel, with the OnTime method in the macro, was crashing in a period between 1 hour (minimum crash) and 7 hours (maximum length of time before crashing). I was forced to open a distance connection from home and running it in the morning before to go out, to make sure the job was done before I got at work.
- my same macro, with the procedure I have described you above, never crashed anymore. I believe the system process is much more reliable than the Excel's one (but again, I don't have any evidence to prove you this so don't take it as gold).

Can you schedule periodic data imports in VBA?

I have a VBA function that, when invoked via a button on a sheet, extracts a .csv file from a website and imports it into my workbook. The problem is the file is uploaded in different time intervals during the day, so I have to periodically check the website.
Is there a way in VBA that I can schedule when a function is invoked?
You can use the Application.OnTime() method which will allow you to schedule a Macro for a specific time amongst other settings. However you're probably best using VB Script or similar.
It's also important to note that the workbook would need to be open the entire time if you are going to use VBA, whereas a VBS file will run silently and can be triggered to run at startup.
If you're proficient enough with VBA then you shouldn't have a problem writing VBS.

Windows Task Scheduler running an Excel VBA script

I have a VBA script in Excel that I wish to run every morning. I want to use the Windows task scheduler to run the VBA script.
I currently use windows task scheduler to run an automated MATLAB process. I have done this by putting the path of the MATLAB exe in the Program/script box and putting other parameters in the Add arguments & Start in text boxes. Is it possible to do something similar with Excel & if so how?
I have seen lots of people mention writing a VBA script which I do not wish to do.

Schedule Outlook 2003 Macro

I have an Outlook 2003 macro written in VBA.
I would like to schedule it so that it automatically runs at a certain time, say 01:00. The PC is always logged on.
I couldn't find anything which suggests how to do this.
You could create a scheduled task on the PC to run daily at the specific time, and use the Outlook Command Line Switch to start the program and call your macro e.g:
"C:\Program Files\Microsoft Office\Office11\Outlook.exe" /autorun macroname
Update:
You may be able to use an API call to start a timer to run in the background of outlook, you could combine this with the VB Now() command to get the current time and if greater than your scheduling time you could trigger your macro, the API example can be found here:
http://www.outlookcode.com/threads.aspx?forumid=4&messageid=22831
http://support.microsoft.com/kb/237913/EN-US
This article says it can't be done unless you rewrite it to use CDO.
Sorry.