I have This in My ThisWorkbook
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
With ThisWorkbook.Worksheets("My_Subs")
Application.Run "CommandButton2_Click"
End With
End Sub
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:00:30"), "Save1"
End Sub
And this in Module1
Sub Save1()
Dim path1 As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.EnableEvents = False
Sheets("My_Subs").Select
Cells.Select
Selection.ColumnWidth = 12
path1 = "\\ant.amazon.com\dept-as\hyd\hyd1\HR\NA-ERC\ERP Workallocation\Work Extraction- Asc Files\Associate Data\" & Environ("username") & ".xlsx"
If Len(Dir(path1, vbDirectory)) = 0 Then
Set wkb = Workbooks.Add
wkb.SaveAs path1
End If
Set destwb = Workbooks.Open(path1)
Set dest_sheet = destwb.Worksheets("Sheet1")
Set currSheet = ThisWorkbook.Sheets("My_Subs")
dest_sheet.Cells.Delete
currSheet.Cells.Copy _
Destination:=dest_sheet.Cells
destwb.Save
destwb.Close
Application.OnTime earliesttime:=Now + TimeValue("00:00:30"), Procedure:="Save1"
ThisWorkbook.Save
End Sub
Private Sub CommandButton2_Click()
MsgBox "Please Make Sure Your copy is Saved"
Application.OnTime earliesttime:=Now + TimeValue("00:10:00"), Procedure:="Save1"
Schedule = False
MsgBox "Successful"
End Sub
The problem i'm facing is i have to use few different sheets and one workbook has be saved for every 10 minutes. so i've used OnTime to save. So thisworkbook calls Save1 once the workbook is open. Save1 recalls itself to run for every 10 minutes.
I've added this Beforeclose code to stop ontime macro before close. But this applies to all workbooks open and doesn't run when any other workbook is open. this triggeres the ontime code and reopens my sheet which has already been closed.
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
With ThisWorkbook.Worksheets("My_Subs")
Application.Run "CommandButton2_Click"
End With
End Sub
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:00:30"), "Save1"
End Sub
And this in Module1
Sub Save1()
Dim path1 As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.EnableEvents = False
Sheets("My_Subs").Select
Cells.Select
Selection.ColumnWidth = 12
path1 = "\\ant.amazon.com\dept-as\hyd\hyd1\HR\NA-ERC\ERP Workallocation\Work Extraction- Asc Files\Associate Data\" & Environ("username") & ".xlsx"
If Len(Dir(path1, vbDirectory)) = 0 Then
Set wkb = Workbooks.Add
wkb.SaveAs path1
End If
Set destwb = Workbooks.Open(path1)
Set dest_sheet = destwb.Worksheets("Sheet1")
Set currSheet = ThisWorkbook.Sheets("My_Subs")
dest_sheet.Cells.Delete
currSheet.Cells.Copy _
Destination:=dest_sheet.Cells
destwb.Save
destwb.Close
Application.OnTime earliesttime:=Now + TimeValue("00:00:30"), Procedure:="Save1"
ThisWorkbook.Save
End Sub
Private Sub CommandButton2_Click()
MsgBox "Please Make Sure Your copy is Saved"
Application.OnTime earliesttime:=Now + TimeValue("00:10:00"), Procedure:="Save1"
Schedule = False
MsgBox "Successful"
End Sub
The problem i'm facing is i have to use few different sheets and one workbook has be saved for every 10 minutes. so i've used OnTime to save. So thisworkbook calls Save1 once the workbook is open. Save1 recalls itself to run for every 10 minutes.
I've added this Beforeclose code to stop ontime macro before close. But this applies to all workbooks open and doesn't run when any other workbook is open. this triggeres the ontime code and reopens my sheet which has already been closed.