AboutStuart Resnick Expertise I can answer any question relating to MS Excel formulas, or to programming with vba (Visual Basic for Applications) in the Excel environment
Experience As a consultant, I've designed Excel tools since the 90s, working for the Federal Reserve Bank, AT&T, and (currently) Gap Inc.
Expert: Stuart Resnick Date: 7/15/2008 Subject: Disabling the Macro
Question Hi,
I have created a command button in the excel which once clicked will close the worksheet. The user should only use this button, for this I need to deactivate the the X button appearing on the right top of the workbook.
It would be great if you could help me out.
Thanks
Dev
Answer Attach this VBA code to the ThisWorkbook object:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = True
End Sub
This disables any attempt to close the workbook. Then, in the VBA code attached to the button that you want to actually close the workbook, you can bypass the above "cancel" code as follows:
Private Sub CommandButton1_Click()
Application.EnableEvents = False
ThisWorkbook.Close
End Sub