How to save an Excel file every, say one minute? - excel

Are there some macros which can do it?
Any help would be appreciated - I am novice with VBA.

Please put this code in ThisWorkbook module. you can access this module by pressing double click on ThisWorkbook module inside VBA project.
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:01:00"), "Save1"
End Sub
then put this code in standard module. To insert standard module, right click on VBA project==>Insert==>Module. then paste code in newly created module.
Sub Save1()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.OnTime Now + TimeValue("00:01:00"), "Save1"
End Sub
Now when you open your workbook, it will automatically save every minute. You need to reopen Workbook in order to trigger Workboook_Open event.

Related

Run WorkBook Calculate Formulas as last process

how I can run Formulas Calculate as last proces when WorkBook is opened ?
I mean when all data are already load, user gave permission to load external data from others WorkBook etc.
I already tryed with
Private Sub Workbook_Open()
Application.Calculate
End Sub
Private Sub Workbook_Open()
If Not Application.CalculationState = xlDone Then
Application.Calculate
End if
End Sub
When I run Excel file and click F9 (Calculate Formulas) then it's work.
How I can run Application.Calculate when all externals links(data from others workbook) are already loaded ? I think it could help
Thanks,
When you open the workbook use
Application.Calculation = xlCalculationManual
When you are about to close the workbook after completing all the work use
ThisWorkbook.Worksheets("wrkSheetName").Calculate
for each worksheet.

Excel Function 'Application.OnTime' still running after closing the workbook

I am using Excel (Office 365).
here is a video with my problem: link
The internet is full of examples where people say that the code should work when we set Schedule to False but sadly that did not help me.
The code should start a cycle using Application.OnTime and stop it before closing the workbook.
If I run the function stopF manually, in the VBA Editor, it stops Application.OnTime properly.
But if I close the workbook, it still opens up and continues executing Application.OnTime.
' In Module1:
Public startingTime As Double
Sub startF()
MsgBox "hello"
startingTime = Now + TimeValue("00:00:10")
Application.OnTime startingTime, "Module1.startF"
End Sub
Sub stopF() 'HERE IT WORKS GOOD
On Error Resume Next
Application.OnTime startingTime, "Module1.startF", , False
End Sub
' In ThisWorkbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call stopF 'HERE IT DOES NOT WORK GOOD
End Sub
I don't have a clue what could I do more.
My temporary solution to my question:
I left the whole code like it was and added into Module1 following code:
' In Module1:
Sub closeWorkbook()
Call stopF
ThisWorkbook.Close
End Sub
The point is:
for manual closing stopF called from Workbook_BeforeClose will work
for vba closing stopF called from a module will work.
A bit more detailed:
when a user closes a workbook manually (closes the Excel window) then Workbook_BeforeClose => Call stopF will be triggered and it will stop Application.OnTime properly.
if I want to close the workbook via code then I do not rely on the Workbook_BeforeClose function (because it doesn't work for this somehow) but I call my new closeWorkbook() function when needed, which does terminate Application.OnTime and then closes the workbook.

Auto-save in VBA is reopening closed files

I implemented some code into a few of my excel sheets that would cause the file to autosave periodically. The issue I am running into with the code, is that when it is executed, it reopens the files that have been closed that also contain the same code.
I am looking for a way to have VBA autosave documents every so often, but it would no longer run if the file isn't open.
This is the code I have implemented:
Contained in "ThisWorkbook":
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("03:00:00"), "Save1"
End Sub
Contained in "Module 3":
Sub Save1()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.OnTime Now + TimeValue("03:00:00"), "Save1"
End Sub
A note: The code between all of the documents is 100% identical (except the TimeValue, which varies by a few hours among some of them).
Try the next approach, please:
Adapt the OnTime call to a better qualified Sub:
Private Sub Workbook_Open()
scheduleTime = Now + TimeValue("03:00:00")
Application.OnTime scheduleTime, "Module3.Save1"
End Sub
Make the Sub in discussion Private, and create a new Public variable on top of the module:
Public scheduleTime As Date
Private Sub Save1()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
scheduleTime = Now + TimeValue("03:00:00")
Application.OnTime scheduleTime, "Module3.Save1"
End Sub
Clear the already set OnTime procedure:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime scheduleTime , "Module3.Save1", , False
End Sub
The first two items will solve the opening of other workbooks when the active workbook procedure is called. But, this should not happen and I am afraid that this is not the real opening mechanism, the workbooks are open by their own OnTime function call.
If my above supposition is True, the item 3 alone will solve the issue...
As you may notice, OnTime is an Application method, not a Workbook method.
This means that, even if you close the Workbook, so long as the Excel Application is still open that OnTime is still counting down. Once it hits zero, it will try to run the Subroutine - but the workbook is closed. Rather than throw an Error, Excel will reopen the Workbook, and then run the Subroutine.
In order to stop this, you will need to unscheduled the Subroutine before you close the Workbook, which will also mean Storing the time. So, you will need a Module that looks something like this:
Option Private Module
Option Explicit
Private AutoRunTime AS Date
Public Sub AutoSaveWorkbook()
ThisWorkbook.Save
AutoRunTime = Now()+TimeSerial(3,0,0) '3 hours
Application.OnTime AutoRunTime, "AutoSaveWorkbook", True
End Sub
Public Sub CancelAutoSave()
Application.OnTime AutoRunTime, "AutoSaveWorkbook", False
End Sub
Then, in the Workbook_Open even you call AutoSaveWorkbook to start things off, and in the Workbook_BeforeClose even you call CancelAutoSave to end them

Open Excel to Calculate and Close Excel Macro

I have file excel contained macro (.xlsm file). This Macro is objected to calculate the workbook and close the excel application.
Sub calc()
ActiveWorkbook.Calculate()
ActiveWorkbook.Saved = True
Application.Quit
End Sub
I want to trigger calc() macro every time i open the excel. So in ThisWorkbooks, i add
Private Sub Workbook_Open()
Call calc
End Sub
I stuck, when i open the .xlsm file, macro is calculate but the .xlsm won't close at all. Any suggestion?
It seems you have a syntax error in your code. You should also pay attention to the hint from #Tarik. The following code should do what you want
Private Sub Workbook_Open()
Call calc
End Sub
Sub calc()
Calculate
ThisWorkbook.Save
Application.Quit
End Sub

VBA Application.Ontime error 438. Object doesn't support this property or method

I've got a macro that has been working absolutely fine in Excel 2010. I'm now trying to run this file using Excel 2016 / 365 and the macro pops up with a run time error 438: Object doesn't support this property or method. It basically closes and re-opens the current Excel file that I'm working on ignoring any messages to save along the way and regardless of whether I'm read-only or not. If anyone can help then that would be fantastic. The code is below. Thanks in advance.
Sub reopen2()
Application.DisplayAlerts = False
Dim wb As Excel.Workbook
Set wb = ThisWorkbook
Dim pth As String
pth = wb.FullName
Application.OnTime Now + TimeValue("00:00:01"), Application.Workbooks.Open(FILENAME:=pth, Notify _
:=False)
wb.Close (False)
Application.DisplayAlerts = True
End Sub
Application.OnTime's second parameter is explicitly declared as string, so this:
Application.OnTime Now + TimeValue("00:00:01"), Application.Workbooks.Open(FILENAME:=pth, Notify _
:=False)
is actually:
Application.OnTime Now + TimeValue("00:00:01"), Application.Workbooks.Open(FILENAME:=pth, Notify _
:=False).DefaultPropertyOfWorkbook
A workbook does not have a default property, hence the "Object doesn't support this property or method" error.
You have to put the code into a method and provide its name to OnTime as a string.
You want to pass the already closed workbook's full path to that method, so normally you would have to register the OnTime call with parameters.
In your case, though, you don't have to do that because all you want to do is open the workbook, and Excel remembers in which workbook the registered macro resides, and if scheduled to be called on time, it will reopen the workbook automatically just to call the macro, which is already the effect that you want. So your complete code would be:
Option Explicit
Sub reopen2()
Application.OnTime Now + TimeValue("00:00:01"), "DoNothing"
ThisWorkbook.Close False
End Sub
Public Sub DoNothing()
End Sub
provided it's in a regular module.

Resources