Forum Discussion

cking1333's avatar
cking1333
Copper Contributor
Oct 08, 2024

Disable (X) Button and Force Users to Close Workbook through Userform

Hi....I found code that prevents users from closing a workbook using the X button however when they attempt to close the workbook the correct way (through a userform) it does not close. This is the code I found to prevent using the X button:

 

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = True
MsgBox "You must use the EXIT ARMS button to close ARMS at all times."
End Sub

 

This is the userform code I have:

Dim AnswerYes As String

    ActiveWorkbook.Unprotect "unlockme13"
    Sheets("HOME PAGE").Unprotect Password:="unlockme"

    AnswerYes = MsgBox("Do you want to save & close ARMS?", vbQuestion + vbYesNo, "User Repsonse")

    If AnswerYes = vbYes Then
            Unload Me
            For Each ws In ActiveWorkbook.Worksheets
            ws.Visible = xlSheetVisible
        Next ws
            
            Sheets("UserAccess").Visible = False
            Sheets("MAX VALUES").Visible = False
            Sheets("TIER ACCESS").Visible = False
            Sheets("LISTS").Visible = False
            Sheets("CLIENTS").Visible = False
            
            Range("M1").Value = ""
            Range("M3").Value = ""
            Range("M6").Value = ""
            
    Sheets("HOME PAGE").Protect Password:="unlockme"
    ActiveWorkbook.Protect "unlockme13"
            
        ThisWorkbook.Save
        Application.Quit
    Else
        ThisWorkbook.Close SaveChanges:=False
        Application.Quit
    End If

 

Not sure why this is happening so any assistance would be appreciated. 

 

Thanks!

Resources