Help! Where does this code go?

zakasnak

Active Member
Joined
Sep 21, 2005
Messages
308
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS
Do I put this in the worksheet tab "View Code"? That's where it would make sense to me....

I can't tell that it's working, tho.

Code:
Private Sub Workbook_Open()
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
Dim Start, Finish, TotalTime, TotalTimeInMinutes, TimeInMinutes
Application.DisplayAlerts = True
TimeInMinutes = 30 '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
    <?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
End Sub
 
If you look at the code it manipulates TimeInMinutes to get total time. Try replacing

TimeInMinutes=30

with

TimeInMinutes=6

It should then give you a message in about 36 minutes from opening the workbook.

... so if it says 180 minutes, it really means 180+30 minutes? :confused:
 
Upvote 0
No, this is the relevant part of the code

Code:
TotalTimeInMinutes = (TimeInMinutes * 60) - (5 * 60)
 
Upvote 0
No, this is the relevant part of the code

Code:
TotalTimeInMinutes = (TimeInMinutes * 60) - (5 * 60)


So : TotalTimeInMinutes = (180 * 60) - (5 * 60) = 10500 (MINUTES ... or SECONDS?)

I see that further down in the code, it divides that # by 60 & says it's been open for xMinutes.... or 175.

Am I reading it correctly?
 
Last edited:
Upvote 0
So ... I've left this test sheet open for almost an hour & no message box & no closing of Excel...

What am I doing wrong?
 
Upvote 0
Tushar's code is undoubtedly better but try replacing

If TimeInMinutes > 5 Then

with

If TimeInMinutes > 1 Then

and enter a short time. It looks like the code should work but I haven't tested it.
 
Upvote 0

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