Help with Autosave macro when multiple workbooks open

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
Hi,
i have the following code which works great with just the one open workbook. But it runs into problems if other excel workbooks are open

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)   Application.OnTime dTime, "autoSave", False
End Sub
 
Private Sub Workbook_Open()
    dTime = Now + TimeValue("00:01:00")
    Application.OnTime dTime, "autoSave"
End Sub

Code:
Public dTime As DateSub autoSave()
    dTime = Now + TimeValue("00:01:00")
    Application.OnTime dTime, "autoSave"
    
    ActiveWorkbook.Sheets("Sheet1").Range("A1") = Time()
    ActiveWorkbook.Save
     
    
End Sub

i know i have to declare WB somewhere, maybe on open? so it stores in memory. I cant declare in autosave() as when it runs it will use the current active workbook

any help appreciated
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
when i try the following it errors after a minute with "object required"

Code:
Public wb1 As Workbook

Private Sub Workbook_BeforeClose(Cancel As Boolean)
   Application.OnTime dTime, "autoSave", False
End Sub
 
Private Sub Workbook_Open()


Set wb1 = ActiveWorkbook
    dTime = Now + TimeValue("00:01:00")
    Application.OnTime dTime, "autoSave"
End Sub

Code:
Public dTime As Date
Sub autoSave()
    dTime = Now + TimeValue("00:01:00")
    Application.OnTime dTime, "autoSave"
    
    wb1.Sheets("Sheet1").Range("A1") = Time()
    wb1.Save
     
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,955
Messages
6,175,605
Members
452,660
Latest member
Zatman

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