View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Leanne Leanne is offline
external usenet poster
 
Posts: 87
Default How to enter a macro that performs when a file is closed

This I did not know. Thanks for this information I will use it in future to
try and answer my questions first.

"Rick Rothstein (MVP - VB)" wrote:

Event procedures are a special kind of subroutine... the argument list for
it has a required structure and you can't change it; however, you don't have
to remember what the individual argument lists for the various events are.
When you go to the code window for a worksheet or, as in this case, the
workbook, the drop down to the left in the title bar area of the code window
allows you to select the Worksheet or Workbook (as the case may be). For the
workbook code window, selecting Workbook from that list populates the drop
down on the right side of the title bar area with all of its available
events. In that drop down, you will see the BeforeClose event and if you
select it, it will create the event subroutine header for you. That header
is...

Private Sub Workbook_BeforeClose(Cancel As Boolean)

The Cancel argument (a Boolean, hence it is either True or False) is a
method the event gives you to communicate with the code that triggered the
event. If, inside the BeforeClose event, you set Cancel equal to True, it
tells Excel to not Close. This means that you can create code to exam
certain situations you deem important (for example, having a certain cell
filled in or not) and cancel the close operation the user initiated if those
situations warrant it. Of course, good programming practice would be to
include code that pops a message box up and tells the user his/her close
request is being denied and why.

Rick


"Leanne" wrote in message
...
Hi Mike,

Thank you! I knew the 'sheet visible' bit but did not know the before
close
event.

Out of interest - what does this do/mean (Cancel As Boolean)


"Mike H" wrote:

Use the before_close event
Alt+Fll to open VB editor. double click 'This workbook' and paste this in

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Sheet1").Visible = False
End Sub

Mike

"Leanne" wrote:

I want a sheet to be hiden when the file is closed.

I have done the code to unhide it should the user require but because
of
this I want it hiden again when the file is closed (if it has been
unhiden)

Can any one tell me where/how I write this?