Auto Close a Workbook

Steveo59

Board Regular
Joined
Oct 19, 2007
Messages
204
Is there anyway that a workbook can be set up to close itself at a set time i.e 19.00hrs every day?
 

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.
Hi,

You can use the OnTime method for this.

Pop this on your ThisWorkbook page in the workbook's VBA project:

Code:
Private Sub Workbook_Open()

Application.OnTime TimeValue("19:00:00"), "close_me"

End Sub

And then this in a standard module in the project:

Code:
Sub close_me()

ThisWorkbook.Close Savechanges:=True

End Sub

It will close the workbook at 19:00 and save any changes.

Dom
 
Upvote 0
Change the first bit of code to this:

Code:
Private Sub Workbook_Open()

Application.OnTime Now + TimeValue("00:10:00"), "close_Me"

End Sub

Dom
 
Upvote 0
dom
i want to ask u about ur code
how is this code run automaticly
when work book open
cuz it is name
Private Sub Workbook_Open
i don't know how it is happen
i am sorry for this question
 
Upvote 0
It's what is known as a 'workbook event'. There a number of events that can automatically be called to execute when specific things happen within a workbook.

In the VBA editor if you view the ThisWorkbook code page of an Excel file there will be 2 drop downs at the top of the page. Change the left hand one to say Workbook and then the right hand one will display a list of events that are available.

If you select one it will pop the first and last line of the event's code onto the sheet for you and then you can just pop whatever code you want to kick off when that event triggers within those lines.

There are also numerous 'worksheet events' that are available within each worksheet's code page.

Hope that makes sense.

Dom
 
Upvote 0
There are also numerous 'worksheet events' that are available within each worksheet's code page.

i think auto_close on of this events
how i can know more events
 
Upvote 0
Hi,

You can use the OnTime method for this.

Pop this on your ThisWorkbook page in the workbook's VBA project:

Code:
Private Sub Workbook_Open()

Application.OnTime TimeValue("19:00:00"), "close_me"

End Sub

And then this in a standard module in the project:

Code:
Sub close_me()

ThisWorkbook.Close Savechanges:=True

End Sub

It will close the workbook at 19:00 and save any changes.

Dom

Thanks Domski :)
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,484
Members
448,967
Latest member
visheshkotha

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