Question: Macro to save and close workbook

extremecorvette

New Member
Joined
Jun 2, 2011
Messages
20
Question: Macro to save and close workbook
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
This is what I have come up with, I have found three issues with it.
  • One it closes all the workbooks I have open and only saves the one that the macro is in.
  • Two if I put this macro in more than one workbook I can’t have both open at the same time.
  • Three I got the missage to come up You have 5 minutes to save before Excel closes. But if no one clicks that message the workbook stays open.
<o:p> </o:p>
Running Excel 2003 with Windows 7
<o:p> </o:p>
<o:p> </o:p>
Private Sub Workbook_Open()
<o:p> </o:p>
Dim Start, Finish, TotalTime, TotalTimeInMinutes, TimeInMinutes
Application.DisplayAlerts = True
TimeInMinutes = 600
If TimeInMinutes > 5 Then
TotalTimeInMinutes = (TimeInMinutes * 60) - (5 * 60)
Start = Timer
Do While Timer < Start + TotalTimeInMinutes
DoEvents
<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:place w:st="on">Loop</st1:place>
Finish = Timer
TotalTime = Finish - Start
Application.DisplayAlerts = False
MsgBox "This file has been open for " & TotalTime / 60 & " minutes. You have 5 minutes to save before Excel closes."
End If
<o:p> </o:p>
Start = Timer
Do While Timer < Start + (5 * 60)
DoEvents
<st1:place w:st="on">Loop</st1:place>
Finish = Timer
TotalTime = Finish - Start
Application.DisplayAlerts = False
MsgBox "Excel will now close."
Application.Quit

ActiveWorkbook.Close True
' closes the active workbook and saves any changes
End Sub

<o:p> </o:p>
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
The Macro will auto save and close the workbook after a set time period. See I have 600 minutes in there. When 5 min is left it will pop up a box saying You have 5 minutes to save before Excel closes. You have to click the box to get it to go away and get the timer to resume.

So what's it supposed to do? Dave
 
Upvote 0
i have been looking for something like this and pasted it into my module

.... where is the times displayed? im confused

thanks
 
Upvote 0
It doesn't display the times... just the warning msgbox. Instead of the using the msgbox line of code, call this macro and the msgbox will disappear in 5 seconds. HTH. Dave
Code:
Sub MsgBox5sec()
Dim WsShell
Dim intText As Integer
Set WsShell = CreateObject("WScript.Shell")
intText = WsShell.Popup("You have 5 minutes to save before Excel closes!", _
                              5, "WARNING") '5 is seconds
Set WsShell = Nothing
End Sub
 
Upvote 0
Yes as per your code the msgbox shows at the 5 min mark. The msgbox disappears by itself after 5 seconds which seems to address your 3rd concern. Dave
 
Upvote 0
I set it up to run 2 min and then at 1 min give the warning. It would be nice to get the WsShell.Popup to stay open for 30 sec.

I still need to find a way to only close this one workbook and not all the others I have open. Any ideas?


Here is the code I'm using.


Code:
Private Sub Workbook_Open()
Dim Start, Finish, TotalTime, TotalTimeInMinutes, TimeInMinutes
Application.DisplayAlerts = True
TimeInMinutes = 2
If TimeInMinutes > 1 Then
TotalTimeInMinutes = (TimeInMinutes * 60) - (1 * 1)
   Start = Timer
   Do While Timer < Start + TotalTimeInMinutes
       DoEvents
   Loop
   Finish = Timer
   TotalTime = Finish - Start
   Application.DisplayAlerts = False
Dim WsShell
Dim intText As Integer
Set WsShell = CreateObject("WScript.Shell")
intText = WsShell.Popup("You have 1 minutes to save before Excel closes!", _
                              1, "WARNING") '60 is seconds
Set WsShell = Nothing
End If
Start = Timer
   Do While Timer < Start + (1 * 60)
       DoEvents
   Loop
   Finish = Timer
   TotalTime = Finish - Start
   Application.DisplayAlerts = False
   MsgBox "Excel will now close."
   Application.Quit
   
   ActiveWorkbook.Close True
' closes the active workbook and saves any changes
End Sub
 
Upvote 0
Code:
intText = WsShell.Popup("You have 1 minutes to save before Excel closes!", _
                              30, "WARNING") '30 is seconds (msgbox timer)

Take out the application.quit and only use the activeworkbook.close. HTH. Dave
 
Upvote 0
Ok this code now works for closing just the workbook I want to close but its not auto saving any of the changes I made before closing.

Code:
Private Sub Workbook_Open()
Dim Start, Finish, TotalTime, TotalTimeInMinutes, TimeInMinutes
Application.DisplayAlerts = True
TimeInMinutes = 2
If TimeInMinutes > 1 Then
TotalTimeInMinutes = (TimeInMinutes * 60) - (1 * 1)
   Start = Timer
   Do While Timer < Start + TotalTimeInMinutes
       DoEvents
   Loop
   Finish = Timer
   TotalTime = Finish - Start
   Application.DisplayAlerts = False
Dim WsShell
Dim intText As Integer
Set WsShell = CreateObject("WScript.Shell")
intText = WsShell.Popup("You have 1 minutes to save before Excel closes!", _
                              30, "WARNING") '30 is seconds (msgbox timer)
Set WsShell = Nothing
End If
Start = Timer
   Do While Timer < Start + (1 * 60)
       DoEvents
   Loop
   Finish = Timer
   TotalTime = Finish - Start
   Application.DisplayAlerts = False
   MsgBox "Excel will now close."
   ActiveWorkbook.Close
   
   ActiveWorkbook.Close True
' closes the active workbook and saves any changes
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,525
Messages
6,179,317
Members
452,905
Latest member
deadwings

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