View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
John John is offline
external usenet poster
 
Posts: 2,069
Default Run macro brfore close

Code below needs to be placed in This WorkBook - From workbook launch VBE
using short-key Alt+F11. On the left treeview for
this project double click This Workbook and paste the code there.

Private Sub Workbook_BeforeClose(Cancel As Boolean)

msg = "Update File?"
Title = "File Update"

response = MsgBox(msg, vbYesNoCancel + vbQuestion, Title)

If response < vbNo Then

'exit without saving
Saved = True

ElseIf response = vbCancel Then

'cancel close
Cancel = True

Exit Sub

Else

'run update your procedure
Run fileupdatecode

msg = MsgBox("File Updated", vbInformation, Title)

'line below assumes your file update code saves changes?
'delete the line if it does not otherwise workbook
'closes without saving the changes
Saved = True

End If


End Sub
--
jb


"Al" wrote:

Hello
I would like to run this code before the file is closed.

Sub Update()

Msg = "Update File?"
Title = "File Update"
Response = MsgBox(Msg, vbYesNoCancel + vbQuestion, Title)
If Response = vbNo Then
Exit Sub
End If

If Response = vbCancel Then
Exit Sub
End If

Run file update code

MsgBox "File Updated"


End Sub

How do I do this?
Thanks