Autosave workbook

adamsm

Active Member
Joined
Apr 20, 2010
Messages
444
Hi anyone,

I'm trying to write a macro code that would auto save my workbook every minute.

Any help on this would be kindly appreciated.

Thanks in advance.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

adamsm

Active Member
Joined
Apr 20, 2010
Messages
444
Thanks for the help. But how do I place the last save time in cell A1 of any sheet of the workbook?
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try in the ThisWorkbook module

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets("Sheet1").Range("A1").Value = Now
End Sub
 

adamsm

Active Member
Joined
Apr 20, 2010
Messages
444

ADVERTISEMENT

Thanks for the help.

But with the line you have given I'm getting debug message that highlights the line of code in the This Workbook Module;

Application.OnTime dTime, "MyMacro", , False

How could I avoid this?

Here is the code I'm using on my This Workbook Module
Code:
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:01:00"), "MyMacro"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime dTime, "MyMacro", , False
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Worksheets("NewMemo").Range("D2").Value = Now
End Sub
And Following is the code I've placed in the standard module
Code:
Public dTime As Date
Sub MyMacro()
Application.ScreenUpdating = False
dTime = Now + TimeValue("00:01:00")
Application.OnTime dTime, "MyMacro"
'YOUR CODE
ThisWorkbook.Save
 Application.ScreenUpdating = True
End Sub
Any help on this would be kindly appreciated.
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650

ADVERTISEMENT

I don't know why you would get that error. The code I gave you does not call the Before_Close macro where that line of code is.
 

adamsm

Active Member
Joined
Apr 20, 2010
Messages
444
Code:
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:01:00"), "MyMacro"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime RunTime, "MyMacro", , False
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Worksheets("NewMemo").Range("D2").Value = Time
End Sub

Code:
Public RunTime As Date
Sub MyMacro()
Application.ScreenUpdating = False
RunTime = Now + TimeValue("00:01:00")
Application.OnTime RunTime, "MyMacro"
'YOUR CODE
ThisWorkbook.Save
 Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
I would recommend you looking at the link I provided. Generally I tend to not trust the OnTime macros, as they can break and, as far as I'm concerned, are unreliable for a long-term solution.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,486
Messages
5,831,968
Members
430,098
Latest member
bemmelen

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
Top