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.
 

Some videos you may like

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

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.
 

Watch MrExcel Video

Forum statistics

Threads
1,108,910
Messages
5,525,585
Members
409,651
Latest member
Quasar Hunter

This Week's Hot Topics

Top