Hello,
I added an activeX button in my workbook to do some stuff and then save and close the file.
Need to disable closing the excel document with the x button or the "file - close" option.
I found some code online that makes sense but because I'm using a boolean variable on workbook I can't access and change that variable within my activeX button close.
So the code would be something like this:
Then the code inside the button would be something like this:
In theory once CloseMode being equal to True would render the If statement on workbook_BeforeClose false and prevent the disabling but it is not doing that. I'm assuming the issue comes from using the variable in two different Subs. I tried defining the variable as a global variable but that didn't work.
Any ideas?
Thanks!
I added an activeX button in my workbook to do some stuff and then save and close the file.
Need to disable closing the excel document with the x button or the "file - close" option.
I found some code online that makes sense but because I'm using a boolean variable on workbook I can't access and change that variable within my activeX button close.
So the code would be something like this:
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not CloseMode Then
Cancel = True
MsgBox "Please use the Close File Button to close the file."
End If
End Sub
Then the code inside the button would be something like this:
Code:
Private Sub ENDCloseButton_Click()
'UNFILTER
Dim ws As Worksheet
On Error Resume Next
For Each ws In ThisWorkbook.Worksheets
If ws.Visible Then ws.ShowAllData
Next ws
On Error GoTo 0
'UNPROTECT
Sheets("IDN").Unprotect
Sheets("BT").Unprotect
Sheets("NPI").Unprotect
Sheets("END").Unprotect
'SAVE AND CLOSE
CloseMode = True
ThisWorkbook.Save
Application.Quit
End Sub
In theory once CloseMode being equal to True would render the If statement on workbook_BeforeClose false and prevent the disabling but it is not doing that. I'm assuming the issue comes from using the variable in two different Subs. I tried defining the variable as a global variable but that didn't work.
Any ideas?
Thanks!