Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
al al is offline
external usenet poster
 
Posts: 363
Default Run macro brfore close

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 320
Default Run macro brfore close

In the VBE, double-click the ThisWorkbook in the Project window, enter this
code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
update 'runs your macro
End Sub

Bob Umlas
Excel MVP

"Al" wrote in message
...
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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Run macro brfore close

From workbook launch VBE using short-key Alt+F11. On the left treeview for
this project double click 'This Workbook' and paste the code to the code
panel.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("Update File?", vbYesNoCancel + vbQuestion, "File Update") _
< vbYes Then Exit Sub
'Run file update code
MsgBox "File Updated"
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,836
Default Run macro brfore close

Bob is right, put your macro in ThisWorkbook. I did this this for a recent
project, to delete any pivot tables that users created and I called another
macro, to make all sheets, but one, hidden, before the workbook closed. Here
is the code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim pt As PivotTable
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.TableRange2.Clear
Next pt
Next ws

Call HideSheets
End Sub

HTH,
Ryan---

--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.


"Bob Umlas" wrote:

In the VBE, double-click the ThisWorkbook in the Project window, enter this
code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
update 'runs your macro
End Sub

Bob Umlas
Excel MVP

"Al" wrote in message
...
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




  #5   Report Post  
Posted to microsoft.public.excel.programming
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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default Run macro brfore close

sorry, minor typo earlier in code:

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


"john" wrote:

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

  #7   Report Post  
Posted to microsoft.public.excel.programming
al al is offline
external usenet poster
 
Posts: 363
Default Run macro brfore close

Thanks!

"Jacob Skaria" wrote:

From workbook launch VBE using short-key Alt+F11. On the left treeview for
this project double click 'This Workbook' and paste the code to the code
panel.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("Update File?", vbYesNoCancel + vbQuestion, "File Update") _
< vbYes Then Exit Sub
'Run file update code
MsgBox "File Updated"
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"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

  #8   Report Post  
Posted to microsoft.public.excel.programming
al al is offline
external usenet poster
 
Posts: 363
Default Run macro brfore close

Thanks!

"Bob Umlas" wrote:

In the VBE, double-click the ThisWorkbook in the Project window, enter this
code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
update 'runs your macro
End Sub

Bob Umlas
Excel MVP

"Al" wrote in message
...
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




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I stop the close method in an auto close macro Paul Excel Programming 2 November 17th 06 02:48 PM
Don't let user close. Use macro button to close? mike Excel Programming 2 October 30th 06 01:31 PM
Do I have to close an odc in a macro? Tachikrusher Excel Programming 2 March 3rd 06 02:23 AM
Run Macro on close Donnie Excel Programming 6 August 29th 05 09:42 PM
On Close macro JudithJubilee[_2_] Excel Programming 14 March 8th 05 09:40 AM


All times are GMT +1. The time now is 11:00 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"