New2ExcelVBA87
New Member
- Joined
- Jun 5, 2014
- Messages
- 13
Hey Everybody,
Not sure if this is already a thread as I've tried searching for answers but come up with only half solutions to what I needed, or incomplete code. I've tried a smattering of code I could find in the forums, and even tried combining bits and pieces, but to no avail. This is mostly due to my lack of understanding in VBA. Any assistance will be greatly appreciated. I'll provide what I'm trying to accomplish below, and I'll provide the code I have.
I will mention that I did find code that did work after I tweaked, and modified it a bit, but I don't understand how it works. I'm attempting to work off of code that I have some understanding of, and this is why I'm reaching out.
I'm trying to auto close out of workbooks for the reports I update as there are users who have them open, and forget to close them at night. I've set the auto close time to 12am.
Below is the code that does not work, but to my understanding/at first glance should work. Any insight would be greatly appreciated.
Private Sub Workbook_Open()
Static SchedSave
If SchedSave <> 0 Then
Application.OnTime SchedSave, "SaveWork", , False
End If
SchedSave = TimeValue("09:29:00") ' Insert Desired time in Military
Application.OnTime SchedSave, "SaveWork", , True
End Sub
Sub SaveWork()
ThisWorkbook.Save
ThisWorkbook.Close
End Sub
Below is the code that does work, but I don't understand it, and I will most definitely utilize it until I find a different solution. However I feel like this code is a bit overly complicated? vs. possibly creating code that can just be in one place (ThisWorkbook) instead of being in "ThisWorkbook" and Modules. Any insights would be greatly appreciated.
' Insert into ThisWorkbook
Private Sub Workbook_Open()
Reset
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Reset
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Reset
End Sub
' Insert into Module
Sub Reset()
Static SchedSave
If SchedSave <> 0 Then
Application.OnTime SchedSave, "SaveWork", , False
End If
SchedSave = TimeValue("12:00:00") ' 12AM
Application.OnTime SchedSave, "SaveWork", , True
End Sub
Sub SaveWork()
ThisWorkbook.Save
ThisWorkbook.Close
End Sub
Not sure if this is already a thread as I've tried searching for answers but come up with only half solutions to what I needed, or incomplete code. I've tried a smattering of code I could find in the forums, and even tried combining bits and pieces, but to no avail. This is mostly due to my lack of understanding in VBA. Any assistance will be greatly appreciated. I'll provide what I'm trying to accomplish below, and I'll provide the code I have.
I will mention that I did find code that did work after I tweaked, and modified it a bit, but I don't understand how it works. I'm attempting to work off of code that I have some understanding of, and this is why I'm reaching out.
I'm trying to auto close out of workbooks for the reports I update as there are users who have them open, and forget to close them at night. I've set the auto close time to 12am.
Below is the code that does not work, but to my understanding/at first glance should work. Any insight would be greatly appreciated.
Private Sub Workbook_Open()
Static SchedSave
If SchedSave <> 0 Then
Application.OnTime SchedSave, "SaveWork", , False
End If
SchedSave = TimeValue("09:29:00") ' Insert Desired time in Military
Application.OnTime SchedSave, "SaveWork", , True
End Sub
Sub SaveWork()
ThisWorkbook.Save
ThisWorkbook.Close
End Sub
Below is the code that does work, but I don't understand it, and I will most definitely utilize it until I find a different solution. However I feel like this code is a bit overly complicated? vs. possibly creating code that can just be in one place (ThisWorkbook) instead of being in "ThisWorkbook" and Modules. Any insights would be greatly appreciated.
' Insert into ThisWorkbook
Private Sub Workbook_Open()
Reset
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Reset
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Reset
End Sub
' Insert into Module
Sub Reset()
Static SchedSave
If SchedSave <> 0 Then
Application.OnTime SchedSave, "SaveWork", , False
End If
SchedSave = TimeValue("12:00:00") ' 12AM
Application.OnTime SchedSave, "SaveWork", , True
End Sub
Sub SaveWork()
ThisWorkbook.Save
ThisWorkbook.Close
End Sub