closing an application/file automatically after a certain time

Sirish

New Member
Joined
May 7, 2008
Messages
11
Hi All,

I would like to do the following, pls suggest me a vba code.

A test is to be taken on an excel file. Lets say the test is for 30 mins, i would like the excel file should be closed automatically after a duration of 30 mins from the time when it is opened.

Regards
Sirish
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,594
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
Hi Sirish
Copy these into a standard module
Sub TIMER()
Application.Ontime _
EarliestTime:=Now + TimeValue("00:30:00"), _
Procedure:="Ontime"
End Sub

Sub Ontime()
ThisWorkbook.Close True
End Sub

Regards
Michael M
 
Upvote 0

robert phillips

New Member
Joined
Sep 15, 2007
Messages
43
You might want to save the workbook automatically. Open Visual Basic Editor. Put this code in ThisWorkbook. Save the workbook. Close excel. Open the workbook you just saved. It should shut down 5 seconds later(testing for you) without giving a Save File Dialog.

Assuming Sheet1 was the sheet with the questions.


Private Sub Workbook_Open()
Dim TestTime, Total

Sheets("Sheet1").Activate

TestTime = 5 '5 seconds for testing so you can see it operate. Change to 1800

'TestTime = 1800 '30 minutes 1800 secs

Total = Timer + TestTime 'now + 30 min later
Do While Timer < Total
DoEvents 'let the worksheet/user interact
Loop

ThisWorkbook.Save 'your default filename it was opened as

Application.Quit 'end

End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'to prevent file save dialog
SaveAsUI = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,025
Messages
5,984,198
Members
439,877
Latest member
kellylet

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
Top