run macro on workbook close

N

Nigel

Hi,

Can you run a macro when you close a workbook or do you have do this through
a button? i konw you can run a macro on workbook open but i need it the other
way round.
if you can't, can you prevent the workbook being closed via the red crox in
the corner to force use of the button?

thanks in advance,



Nigel
 
G

Gord Dibben

Nigel

In the Thisworkbook Module

Private Sub Workbook_BeforeClose(Cancel As Boolean)

'your code here

End Sub


Gord Dibben Excel MVP
 
T

TJLV

In addition to before close you can use
Auto open. for example when I open the file containing the following macro
all the sheets are hidden except one that makes the usert select a box that
he agrees to the conditions given on the visible page. The accept button
runs a macro that unhides the pages. Since this is done automatically at
opening it effectively does the same thing as hiding the pages before closing.

Sub Auto_open()
'
' Auto_open Macro
' Macro recorded 5/23/2005 by TJLV
'

'
' Sheets("instructions").Select


Sheets("opening").Visible = True
Sheets("instructions").Visible = False
Sheets("reports").Visible = False
Sheets("input").Visible = False
Sheets("Optimize").Visible = False
Sheets("combine").Visible = False
Sheets("calc").Visible = False
Sheets("split").Visible = False
Sheets("Site1").Visible = False
Sheets("Site2").Visible = False
Sheets("Siteagg").Visible = False
Sheets("rates").Visible = False

End Sub
 
Top