OnClose procedure

Flyingmeatball

Board Regular
Joined
Aug 15, 2007
Messages
65
I have a sub for my current workbook set up to call a macro to "tidy things up" before they close the sheet. The problem is the "save excel" dialog is then prompted after these things have run. If the user then presses cancel, they don't actually exit the workbook and are stuck in an ugly limbo of a security sheet I created. Does anyone know what the syntax is to say, "Only cleanup if they are ACTUALLY leaving the workbook, not just if they press the close button". I don't want them to reach the security page by pressing cancel on the saving dialog box. I've included the code below so you can see what I have. I know the sub declaration includes a Cancel as a boolean, but I don't know how to reference them clicking the cancel button triggering a cancel.

Thanks!

Private Sub Workbook_BeforeClose(Cancel As Boolean)

'removes assumptions established on run
Range("D:F").Select
Selection = cb.ClearDataND
Selection.clear

'reset the security sheet
Sheets("Security").Visible = True

'code below hides the "important" sheet
Sheets("mySheet").Visible = xlVeryHidden
'don't worry about this line
cb.SetCBAutoLoad (False)

End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try

Rich (BB code):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.EnableEvents = False
'removes assumptions established on run
Range("D:F").Select
Selection = cb.ClearDataND
Selection.Clear

'reset the security sheet
Sheets("Security").Visible = True

'code below hides the "important" sheet
Sheets("mySheet").Visible = xlVeryHidden
'don't worry about this line
cb.SetCBAutoLoad (False)
Application.EnableEvents = True
End Sub
 
Upvote 0
This is causing my macros that are supposed to run on open and on close to fail. Before on close it would switch to a security page. Upon open it would move away from the security page if they enabled macros. Now its having trouble switching between the two.

Thoughts?

Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,504
Messages
6,125,183
Members
449,212
Latest member
kenmaldonado

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top