AllExperts > Experts 
Search      

Excel

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Excel Answers
Question Library

Ask a question about Excel
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Stuart 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.

 
   

You are here:  Experts > Computing/Technology > Business Software > Excel > Disabling the Macro

Topic: Excel



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

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.