PDA

View Full Version : Help With/Code To Enable Macros . .



OrphanBear
09-18-2009, 07:36 AM
Hi all - I need help. I found the below code on this site. It is perfect for what I needed. However, for some reason when a user clicks that they don't want to save changes, it loops back and aks them the same question again!

My workbook has macros in one sheet module and a standard module. I commented out all the macros but I still get the repeating question. Can anyone help me understand why and fix this? The entire code is located in the Workbook Module. Thank you.


Option Explicit
Const WelcomePage = "Macros"
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False

'Evaluate if workbook is saved and emulate default propmts
With ThisWorkbook
If Not .Saved Then
Select Case MsgBox("Do you want to save the changes you made to '" & .Name & "'?", _
vbYesNoCancel + vbExclamation)
Case Is = vbYes
'Call customized save routine
Call CustomSave
Case Is = vbNo
'Do not save
Case Is = vbCancel
'Set up procedure to cancel close
Cancel = True
End Select
End If

'If Cancel was clicked, turn events back on and cancel close,
'otherwise close the workbook without saving further changes
If Not Cancel = True Then
.Saved = True
Application.EnableEvents = True
.Close savechanges:=False
Else
Application.EnableEvents = True
End If
End With
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False

'Call customized save routine and set workbook's saved property to true
'(To cancel regular saving)
Call CustomSave(SaveAsUI)
Cancel = True

'Turn events back on an set saved property to true
Application.EnableEvents = True
ThisWorkbook.Saved = True
End Sub
Private Sub Workbook_Open()
'Unhide all worksheets
Application.ScreenUpdating = False
Call ShowAllSheets
Application.ScreenUpdating = True
End Sub
Private Sub CustomSave(Optional SaveAs As Boolean)
Dim ws As Worksheet, aWs As Worksheet, newFname As String
'Turn off screen flashing
Application.ScreenUpdating = False

'Record active worksheet
Set aWs = ActiveSheet

'Hide all sheets
Call HideAllSheets

'Save workbook directly or prompt for saveas filename
If SaveAs = True Then
newFname = Application.GetSaveAsFilename( _
fileFilter:="Excel Files (*.xls), *.xls")
If Not newFname = "False" Then ThisWorkbook.SaveAs newFname
Else
ThisWorkbook.Save
End If

'Restore file to where user was
Call ShowAllSheets
aWs.Activate

'Restore screen updates
Application.ScreenUpdating = True
End Sub
Private Sub HideAllSheets()
'Hide all worksheets except the macro welcome page
Dim ws As Worksheet

Worksheets(WelcomePage).Visible = xlSheetVisible

For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVeryHidden
Next ws

Worksheets(WelcomePage).Activate
End Sub
Private Sub ShowAllSheets()
'Show all worksheets except the macro welcome page

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVisible
Next ws

Worksheets(WelcomePage).Visible = xlSheetVeryHidden
End Sub

lucas
09-18-2009, 07:58 AM
At a quick glance it appears that you don't tell it to do anything if they click on no. There is no code in the vbno case:
Case Is = vbNo
'Do not save

OrphanBear
09-18-2009, 08:56 AM
Hi Lucas,

I tried doing this but still no luck . . .



Case Is = vbNo
'Do not save
Application.EnableEvents = True
.Close savechanges:=False
Exit Sub



Also Lucas, I have a number of comboboxes in a worksheet that have code in the change event of those boxes. If I delete those boxes, the code seems to work fine?? Does that make sense?

lucas
09-18-2009, 11:13 AM
You should not turn application events back on as it will cause you to be asked twice if you want to save changes.

Other than that your code seems to work for me. See attached.

Maybe you could clean up your workbook of personal or propiotery info and post it.

OrphanBear
09-18-2009, 12:27 PM
Hi Lucas - I dont' know how to post a worbook. I tried your suggestion but it only worked once. The next time the workbook was opened it seemed that events were turned off. This seemed to work though - not sure why. I just placed a value in cell f1 if user selects "no" . . .



Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False

'Evaluate if workbook is saved and emulate default propmts
With ThisWorkbook
If Range("f1") = 1 Then .Saved = True
If Not .Saved Then
Select Case MsgBox("Do you want to save the changes you made to '" & .Name & "'?", _
vbYesNoCancel + vbExclamation)
Case Is = vbYes
'Call customized save routine
Call CustomSave
Case Is = vbNo
'Do not save
ActiveSheet.Unprotect Key
Range("f1") = 1
ActiveSheet.Protect Key
Case Is = vbCancel
'Set up procedure to cancel close
Cancel = True
End Select
End If

'If Cancel was clicked, turn events back on and cancel close,
'otherwise close the workbook without saving further changes
If Not Cancel = True Then
.Saved = True
Application.EnableEvents = True
.Close savechanges:=False
Else
Application.EnableEvents = True
End If
End With
End Sub

mdmackillop
09-19-2009, 03:06 AM
You can post a workbook using Manage Attachments in the Go Advanced reply section.