Scheduled Autosave VBA code

adelcarpio

New Member
Joined
Apr 3, 2013
Messages
4
I found this code on a spreadsheet at work (a board displayed on a big screen which is on 24/7). Can't get an answer from the owner regarding what exactly it does. It suposedly saves the workbook (amongst other actions) by calling the "UpdateData" Sub at a determined time. I find the first Sub pretty straightforward, but the second one really confuses me :eek:.

Code:
'Macro that runs after board is initially opened.
Sub SaveBoard()
BoardData = TimeValue("21:50:00")
Application.OnTime BoardData, "UpdateData"
End Sub

'Macro that runs each day after the file is opened.
Sub SaveBoardDaily()
BoardData = Now + TimeValue("23:59:59")
Application.OnTime BoardData, "UpdateData"
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
The second function sets the variable BoardData to the next day. The application.ontime function schedules a procedure called "UpdateData" to be run on the time set in BoardData. Somewhere there must be a fucntion called "UpdateData".

I'm pretty sure that's right but I've never personally used this function so can't be 100%
 
Upvote 0
This must be a really bad question for most people but I am new with VBA... and programming

These two functions are strange because
- OnTime: "Returns the decimal number of the time represented by a text string. The decimal number is a value ranging from 0 (zero) to 0.99988426, representing the times from 0:00:00 (12:00:00 AM) to 23:59:59 (11:59:59 P.M.)"
- Now: "Returns the serial number of the current date and time"

I think there must be a mistake in scheduling the time to run "updatedata", Supposedly it should be at only one specific time every day, in this case 21:50:00.

The way I understand what the functions are trying to do is:
Lets say I open the file at 8:00:00 am, the variable is set to initially 21:50:00 (in serial number format) and then immediately to tomorrow's date 7:59:59 am (in serial number format). So, Instead of saving it at 21:50:00 tonight as I would it like to, it will save it at 7:59:59 am, tomorrow!

Am I correct in my interpretation?
 
Upvote 0
The application.ontime function simply runs a procedure at the time given.

The structure of it should be this application.ontime now(), "UpdateData", now()+5

The first now() defines the earliest that the application should run the procedure. If excel is in busy at the time it will not run the procedure until the latest time. in this case now()+5.

The first function when run will set "UpdateData" to run at 2150.

The second function when run will set "UpdateData" to run at 24hours from the time that it is run.

What you need to know is when these functions are run. SaveBoard & SaveBoardDaily wont run unless called by another procedure or started by the user.

If they are set to run when the workbook opens then they should be called by an "Auto_Open" procedure.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

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