473,396 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using VBA how do i ensure that the user cannot close excel via....

both the Red X (top right), as well as right clicking the tab on the bottom and closing, or even by pressing ALT+F4??

I understand that there are a lot of threads on various sites with this question however i've been looking for 7 hours yet have no answers. Alot are around forms- this is not the case i simply dont want the user to manually close Excel, they must do it via my own close button (which is linked to a macro restoring some settings within my model).

As well as this please would you advise as where to put this code, i.e. new module, existing module? as a macro when the spreadsheet opens??

Thanks for any help in advance,
Paul.
Feb 2 '10 #1
1 1962
Stewart Ross
2,545 Expert Mod 2GB
Hi. You can trap the workbook's BeforeClose event to prevent users closing the workbook unless some condition of your choosing is set.

The event sub is placed not in a global code module but in the code behind the ThisWorkbook component of the VBA project. In the example I tested I cancelled the close event unless a global variable defined in a global code module was set True:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_BeforeClose(Cancel As Boolean)
  2.     If Not blCanClose Then
  3.         Cancel = True
  4.     Else
  5.         ' Your Code to Advise User Here
  6.     End If
  7. End Sub
In the global code module (inserted into the VBA project by selecting Insert, Module from the menu) I placed the following test code:

Expand|Select|Wrap|Line Numbers
  1. Public blCanClose As Boolean
  2.  
  3. Public Sub CloseForm()
  4.     blCanClose = True
  5.     Application.Quit
  6. End Sub
Finally, I placed a command button on the first worksheet and called CloseForm from its On-Click event:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandButton1_Click()
  2.     CloseForm
  3. End Sub
Please note that you may wish to give more thought to providing some form of exit for users if they cannot click your custom exit button - as cancelling the BeforeClose event gives no other direct way out for your users.

-Stewart

PS Instead of cancelling the workbook closure, you could always use the BeforeClose event to run your own exit routine prior to closing - making sure that whatever you want to do is done every time, no matter which route out your users take!
Feb 7 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mike Knight | last post by:
(I've also posted this problem on microsoft.public.excel.programming) I have a MS Access 2003 Database named "AS400 Fields.mdb". This database contains links to tables on an AS400. In MS...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
0
by: Rich Wallace | last post by:
Hello all, Looking for suggestions and tips if possible. I have an application running on a file server that utilizes the FileSystemWatcher to trap when any Excel files are saved by a user. I...
2
by: super_dave_42 | last post by:
I am searching for a way to create multiple records in Access 2000 from a worksheet created in Excel. I'm pretty much self-taught with Visual Basic so forgive me if this poorly described. Basically...
3
by: RJN | last post by:
Hi I've a template excel file which has all the calculations defined. There are certain input values to be entered which gives a lot of output to the user. I don't want to expose the excel sheet...
2
by: Robert Bravery | last post by:
Hi all, Being new to C# and .net I often don't know how to use things. I have created an app that imports excel data, it works well, with methods to open excel, extract the data and close excel....
1
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access...
0
by: Fendi Baba | last post by:
I created an ASP.net page which opens excel. This works fine in Office 2007. However, Excel 2003 runs in an error which states "cannot read output file". Hee is the VB code and below is the ASPX...
2
by: | last post by:
I am using a datareader to cycle through a list of records one at a time. I frequently get connection timeouts and am wondering what I am doing wrong. In more detail, I retrieve an excel...
7
by: =?Utf-8?B?VGVycnkgSG9sbGFuZA==?= | last post by:
I have a vb.net app that opens an excel worksheet, reads data and then closes the sheet. Im noticing that the Excel process is still running after I have closed and disposed of my excel objects. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.