Help with Before Close event

exceluser66

New Member
Joined
Nov 11, 2016
Messages
4
I have a macro enabled file that unhides worksheets when a password is entered on the first sheet. Works like a charm. The problem is on closing. On close, the user has the option of saving, not saving or continuing (yes/no/cancel). I want to hide all the sheets except the first one when the file is closed regardless of which option is chosen. In other words, if the user chooses to not save and close, I still want the sheets to hide and go back to the first worksheet. I am using the Before Close event. Clicking No does not save the file but also does not hide the sheets. is this possible?

I am not a coder by trade; self taught VBA using this forum! thanks in advance. :)
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Can we see your current code?
 
Upvote 0
Rich (BB code):
Private Sub Workbook_BeforeClose(Cancel As Boolean)<o:p></o:p>Dim answer As String<o:p></o:p>
 answer = MsgBox("Do youwant to save your changes?", vbYesNoCancel)<o:p></o:p>
    If answer = vbCancel Then<o:p></o:p>
    Cancel = True<o:p></o:p>
    ElseIf answer = vbNo Then<o:p></o:p>
    ActiveWorkbook.Unprotect"pw"<o:p></o:p>
    Sheets("Sample1").Visible= False<o:p></o:p>
    Sheets("Sample2").Visible= False<o:p></o:p>
    ActiveWorkbook.Protect "pw"<o:p></o:p>
   Sheets("Start").Select<o:p></o:p>
    Application.DisplayAlerts =False<o:p></o:p>
    ThisWorkbook.Close<o:p></o:p>
    Else<o:p></o:p>
    ThisWorkbook.Save<o:p></o:p>
    ActiveWorkbook.Unprotect"pw"<o:p></o:p>
    Sheets("Sample1").Visible= False<o:p></o:p>
    Sheets("Sample").Visible= False<o:p></o:p>
   ActiveWorkbook.Protect "pw"<o:p></o:p>
   Sheets("Start").Select<o:p></o:p>
    ThisWorkbook.Save<o:p></o:p>
    End If<o:p></o:p>
    End Sub<o:p></o:p>
 
Upvote 0
Hi there,

I am logging out at the moment, but just a couple of quick tips. You will want to use BeforeSave or likely AfterSave to enforce that the workbook is only in a Saved state with only the one sheet showing. Google "Excel Force Enable macros' or similar keywords.

Mark
 
Upvote 0
Making it show only the Start sheet after saving is not a problem. The issue is when the user does not want to save the last change made (but has saved the file at least once after opening). In this case, is there a way to NOT save the changes made by the user but closing the file such that only the Start sheet is shown.
 
Upvote 0
...In this case, is there a way to NOT save the changes made by the user but closing the file such that only the Start sheet is shown.

Not by using the BeforeClose event. This is why I suggested using the BeforeSave event as a pseudo AfterSave event (and to maintain backward compatability, as I believe the AfterSave event was added in 2010).

See if this helps: Excelguru Help Site - Force User To Enable Macros

Mark
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,298
Members
449,077
Latest member
Rkmenon

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