|
Close a Workbook after a Time Limit is Reached
This macro will automatically close the workbook in which it is located after that workbook has been opened for a certain period of time. This is a great feature when you are sharing workbooks that can't be open at the same time on more than one computer; in that situation this macro ensures that if someone forgets to close the file, it will automatically close anyway and give others the ability to then work on the file. This is also something which can be used to let you know how many minutes you have left in a certain session, as defined by the total amount of time you should have in the session. Basically, it is a cool way to have a little more control over workbooks which you may send out to other people or use yourself.
Note: Macros must be enabled on the user's computer for this macro to take affect.
Note: To change the amount of time this excel file is allowed to be open, change the " 180" in this line of code: " TimeInMinutes = 180" to any number of minutes that is needed. You can also change the messages that appear in the following two lines of code by simply changing the text only between the quotation marks.
MsgBox "This file has been open for " & TotalTime / 60 & " minutes. You have 5 minutes to save before Excel closes."
MsgBox "Excel will now close."
Where to install the macro: ThisWorkbook
Close a Workbook after a Time Limit is Reached
Private Sub Workbook_Open()
Dim Start, Finish, TotalTime, TotalTimeInMinutes, TimeInMinutes
Application.DisplayAlerts = True
TimeInMinutes = 180 'Timer is set for 180 minutes; change as needed.
If TimeInMinutes > 5 Then
TotalTimeInMinutes = (TimeInMinutes * 60) - (5 * 60)
Start = Timer
Do While Timer < Start + TotalTimeInMinutes
DoEvents
Loop
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
Start = Timer
Do While Timer < Start + (5 * 60)
DoEvents
Loop
Finish = Timer
TotalTime = Finish - Start
Application.DisplayAlerts = False
MsgBox "Excel will now close."
Application.Quit
End Sub
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.
Or For other ways to get there, Click Here.
For Excel Versions Prior to Excel 2007 Go to Tools > Macros > Visual Basic Editor
For Excel 2007 Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic
- On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- Go to the menu at the top of the window and click Insert > Module
- Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
- Go to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Go to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
- Go to Step 8.
- Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.
- You are now ready to run the macro.
Similar Helpful Excel Resources
Hi, im looking for a way to close the workbook after a set time limit but with a message box to pop up to give a warning a minute or so before it does where if you click ok it restarts the process or if no action is taken with the message box after the minute it closes along with the workbook. also if possible can the message box display a countdown of how long left.
i found this macro:
Private Sub Workbook_Open() Dim Start, Finish, TotalTime, TotalTimeInMinutes, TimeInMinutes
Application.DisplayAlerts = True
TimeInMinutes = 180 'Timer is set for 180 minutes; change as needed.
If TimeInMinutes > 5 Then
TotalTimeInMinutes = (TimeInMinutes * 60) - (5 * 60)
Start = Timer
Do While Timer < Start + TotalTimeInMinutes
DoEvents
Loop
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
Start = Timer
Do While Timer < Start + (5 * 60)
DoEvents
Loop
Finish = Timer
TotalTime = Finish - Start
Application.DisplayAlerts = False
MsgBox "Excel will now close."
Application.Quit
End Sub
from http://www.teachexcel.com/free-excel...ime-limit.html which does a lot of what i need so if anyone can guide me in how to adapt this to my needs would be great as im a complete novice.
Any help would be greatly appreciated
I have some VBA code I was able to modify from the forums here to pull data in from multiple sheets. The problem is the data reaches the row limit before the macro finishes so it stops. I copy from 4 sheets (data1, data2, data3, data4) into 3 master files (Inbound, Outbound, Stranded). Is there a way to make excel copy the remaining data onto a second page, like Inbound1, Inbound2. I do it by hand now. The source column to decide what sheet it goes to is Column A and it uses the master file names to decide where it goes.
hello all
i am trying to import data from sql server but
everytime i have more than 60,000 rows it says
not all data can be displayed because i reached the limit
can i increase more rows ?
thanks
Hello Guys,
Can someone help me with simple code. Want to create POP Up when a limit of 20 is reached.
Example:
Say in Excel workbook in column C, i record values, when i scan a barcode. So column c may have 152302, something like that.
Column D will say 1 for each entry. see below,
ColumC D
135422 1
164563 1
456696 1
Like this when it happens 20 times, i want a message to pop up saying" limit reached".
Once i click "OK", it should reset n start doing the same.
I came up with some code from the net like this,
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1").Value >= 20 Then _
MsgBox "LIMIT REACHED", vbOKOnly, "Alert"
End Sub
But this doesnt get reset on its own, as there can be this scanning going to happen "N" number of times.
Can some Expert HELP me... Thanks in advance...
I have a field in my table that is set to a field size of 20. On my entry form, if a user enters more than 20 characters, I want the database to warn of the 20 character limit, and either clear the contents they already typed or throw away anything they typed after the 20.
This is the code I am trying to use:
'
Private Sub txtClassOther_BeforeUpdate(Cancel As Integer)
Dim sOC As String
'validate
If Len(Me.txtClassOther.Value) > 20 Then
sOC = Left(Me.txtClassOther.Value, 20) 'option 1 - set a variable for the first 20 characters.
' Me.txtClassOther.Value = "" 'or option 2 - just clear the contents of the text box
MsgBox "Field can not accept more than 20 characters.", vbOKOnly, "Field Limit Reached"
Me.txtClassOther.Value = sOC 'if option 1 was used, set the textbox to the variable I set earlier.
End If
End Sub
'
'
'
I am getting this error:
Run time error 2115
The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing [the database] from saving the data in this field.
Can anyone help me figure this one out?
Hi,
I have a worksheet with a list of 12000 stock live prices.
I have setup top and bottom price where the spreadsheet display and Alert message in the first column:
My sheet look like this :
"Alerte or Blank" "Stock" "Live Stock Price" " Top limit" " Bottom Limit"
In the alert column I have an if formula.
When I insert a table it does not update automatically when alert appears or disappears.
I have setup a macro which filter the data in another sheet but it keep running and I cannot use excel unless I stop it.
I am looking for any idea that would be better than mine , even if it means exporting the alert somewhere else.
Thanks in advance,
Hi,
Currently using LEN to simply count 50 characters then it tells the user that they need to move onto the next line.
Is it possible with VBA that after 50 characters reached in the selected cell it forces the user into the next cell below and so on in a loop?
Thanks,
hi all
once again, i need your help. and this time the problem is really weird, at least imho.
lets say i have 200 used columns. if i delete 150 of them and then (without saving the file) try to add 100 more i get an excel has reached its limit error. theoretically 200-150+100 = 150 columns, which should be well within the 258 column limit. but excel still gives me the error )its like its calculating 200+100=300 > 258). HOWEVER, if i save the file after deleting, i dont get any errors at all. is this a "feature" of excel and if so is there anyway of circumventing it? because i dont want to save the file prior to adding the columns (i do the whole thing in vba). actually i dont want to save the file at all unless the user click on the save button or choose save/save as from the menu.
|
|