Run macro on exit worksheet/workbook

  • Thread starter Thread starter toontje
  • Start date Start date
T

toontje

I am looking for a macro that opens a msgbox when exit the workbook to
remind you
like "you fill in your working hours?"

is that Private Sub AutoClose() ?

and where do i put it?
 
Hi

Better use the beforeclose event so the user can cancel the close

Copy this in the thisworkbook module

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ans As Long
ans = MsgBox("Do you fill in your working hours?", vbOKCancel)
If ans = vbOK Then
'do nothing
ElseIf ans = vbCancel Then
Cancel = True
End If
End Sub
 
You need Workbook_BeforeClose, as you can Cancel the close

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If your condition not met Then
Cancel=True
End If
End Sub

'This is workbook event code.
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks),
'select View Code from the menu, and paste the code

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
You will need to put it in the workbook code.

Right click on the small picture to the left of the menu item "File"
select "workbook" in the dropdown next to "general"
select "before close" or if you like "before save" in the dropdown next to
"open"
and enter
Msgbox "Heb je je uren ingevuld????"
between the lines already there.

Have fun.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top