PC Review


Reply
Thread Tools Rate Thread

Auto Close Yes no button

 
 
=?Utf-8?B?U2ltb24gLSBNJk0=?=
Guest
Posts: n/a
 
      12th Nov 2007
Hi,

I'm trying to create a Yes No button that activates when the user tries to
close excel, what i want is that if the user selects yes for the workbook to
save and close. If they click No I want them to be able to go back to the
spreadsheet and carry on working. At the moment all i have got is the yes
bit, when they click no the "Do you want to save changes" box appears and i
can't for the life of me get the macro to select cancel.
Here is the code i'm working on at the minute.
Sub Auto_Close()
'
' Auto_Close Macro
' Macro recorded 09/11/2007 by sparrett
'

Application.DisplayAlerts = False
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Have You Saved A copy?"
Style = vbYesNo + vbInformation
Title = "Clear Down Button"
Help = "DEMO.HLP" ' Define Help file.
Ctxt = 1000 ' Define topic
' context.
' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
MyString = "Yes" ' Perform some action


Else ' User chose No.
MyString = "No" ' Perform some action.

Application.DisplayAlerts = False
ThisWorkbook.Saved = False


End If
End Sub

Any help would be appreciated.

Cheers

Simon
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SkxhdGhhbQ==?=
Guest
Posts: n/a
 
      12th Nov 2007
Instead of using Sub Auto_Close() use the workbook's BeforeClose event. It
has a specific Cancel parameter which you can set to TRUE to prevent closing.

To get to the Workbook's code module, right click on the Excel icon
immediately to the left of 'File' in the menu toolbar and choose [View Code]
from the popup list. Choose Workbook from the list at the top (starts off
with General in it) and then choose BeforeClose from the list to the right of
that list.

When you select Workbook from the first list, it will put a stub in for
SelectionChange; you can delete that stub code at any time. When you choose
BeforeClose in the 2nd list, it will provide the stub for that event. You
can put your code within that stub. What you want to do is put a line of
code in the "NO" response part of your msgbox evaluation like this:
Cancel = True
(assumes the question is 'Have you saved a copy?') and you want to remain in
the workbook if a copy hasn't been saved.

When Cancel is set to True, the pending action (Close in this case) is
cancelled.

"Simon - M&M" wrote:

> Hi,
>
> I'm trying to create a Yes No button that activates when the user tries to
> close excel, what i want is that if the user selects yes for the workbook to
> save and close. If they click No I want them to be able to go back to the
> spreadsheet and carry on working. At the moment all i have got is the yes
> bit, when they click no the "Do you want to save changes" box appears and i
> can't for the life of me get the macro to select cancel.
> Here is the code i'm working on at the minute.
> Sub Auto_Close()
> '
> ' Auto_Close Macro
> ' Macro recorded 09/11/2007 by sparrett
> '
>
> Application.DisplayAlerts = False
> Dim Msg, Style, Title, Help, Ctxt, Response, MyString
> Msg = "Have You Saved A copy?"
> Style = vbYesNo + vbInformation
> Title = "Clear Down Button"
> Help = "DEMO.HLP" ' Define Help file.
> Ctxt = 1000 ' Define topic
> ' context.
> ' Display message.
> Response = MsgBox(Msg, Style, Title, Help, Ctxt)
> If Response = vbYes Then ' User chose Yes.
> MyString = "Yes" ' Perform some action
>
>
> Else ' User chose No.
> MyString = "No" ' Perform some action.
>
> Application.DisplayAlerts = False
> ThisWorkbook.Saved = False
>
>
> End If
> End Sub
>
> Any help would be appreciated.
>
> Cheers
>
> Simon

 
Reply With Quote
 
=?Utf-8?B?U2ltb24gLSBNJk0=?=
Guest
Posts: n/a
 
      12th Nov 2007
That has worked a treat.

Thanks Very Much

Simon

"JLatham" wrote:

> Instead of using Sub Auto_Close() use the workbook's BeforeClose event. It
> has a specific Cancel parameter which you can set to TRUE to prevent closing.
>
> To get to the Workbook's code module, right click on the Excel icon
> immediately to the left of 'File' in the menu toolbar and choose [View Code]
> from the popup list. Choose Workbook from the list at the top (starts off
> with General in it) and then choose BeforeClose from the list to the right of
> that list.
>
> When you select Workbook from the first list, it will put a stub in for
> SelectionChange; you can delete that stub code at any time. When you choose
> BeforeClose in the 2nd list, it will provide the stub for that event. You
> can put your code within that stub. What you want to do is put a line of
> code in the "NO" response part of your msgbox evaluation like this:
> Cancel = True
> (assumes the question is 'Have you saved a copy?') and you want to remain in
> the workbook if a copy hasn't been saved.
>
> When Cancel is set to True, the pending action (Close in this case) is
> cancelled.
>
> "Simon - M&M" wrote:
>
> > Hi,
> >
> > I'm trying to create a Yes No button that activates when the user tries to
> > close excel, what i want is that if the user selects yes for the workbook to
> > save and close. If they click No I want them to be able to go back to the
> > spreadsheet and carry on working. At the moment all i have got is the yes
> > bit, when they click no the "Do you want to save changes" box appears and i
> > can't for the life of me get the macro to select cancel.
> > Here is the code i'm working on at the minute.
> > Sub Auto_Close()
> > '
> > ' Auto_Close Macro
> > ' Macro recorded 09/11/2007 by sparrett
> > '
> >
> > Application.DisplayAlerts = False
> > Dim Msg, Style, Title, Help, Ctxt, Response, MyString
> > Msg = "Have You Saved A copy?"
> > Style = vbYesNo + vbInformation
> > Title = "Clear Down Button"
> > Help = "DEMO.HLP" ' Define Help file.
> > Ctxt = 1000 ' Define topic
> > ' context.
> > ' Display message.
> > Response = MsgBox(Msg, Style, Title, Help, Ctxt)
> > If Response = vbYes Then ' User chose Yes.
> > MyString = "Yes" ' Perform some action
> >
> >
> > Else ' User chose No.
> > MyString = "No" ' Perform some action.
> >
> > Application.DisplayAlerts = False
> > ThisWorkbook.Saved = False
> >
> >
> > End If
> > End Sub
> >
> > Any help would be appreciated.
> >
> > Cheers
> >
> > Simon

 
Reply With Quote
 
=?Utf-8?B?SkxhdGhhbQ==?=
Guest
Posts: n/a
 
      12th Nov 2007
Glad I could be of assistance, and thanks for the feedback.

"Simon - M&M" wrote:

> That has worked a treat.
>
> Thanks Very Much
>
> Simon
>
> "JLatham" wrote:
>
> > Instead of using Sub Auto_Close() use the workbook's BeforeClose event. It
> > has a specific Cancel parameter which you can set to TRUE to prevent closing.
> >
> > To get to the Workbook's code module, right click on the Excel icon
> > immediately to the left of 'File' in the menu toolbar and choose [View Code]
> > from the popup list. Choose Workbook from the list at the top (starts off
> > with General in it) and then choose BeforeClose from the list to the right of
> > that list.
> >
> > When you select Workbook from the first list, it will put a stub in for
> > SelectionChange; you can delete that stub code at any time. When you choose
> > BeforeClose in the 2nd list, it will provide the stub for that event. You
> > can put your code within that stub. What you want to do is put a line of
> > code in the "NO" response part of your msgbox evaluation like this:
> > Cancel = True
> > (assumes the question is 'Have you saved a copy?') and you want to remain in
> > the workbook if a copy hasn't been saved.
> >
> > When Cancel is set to True, the pending action (Close in this case) is
> > cancelled.
> >
> > "Simon - M&M" wrote:
> >
> > > Hi,
> > >
> > > I'm trying to create a Yes No button that activates when the user tries to
> > > close excel, what i want is that if the user selects yes for the workbook to
> > > save and close. If they click No I want them to be able to go back to the
> > > spreadsheet and carry on working. At the moment all i have got is the yes
> > > bit, when they click no the "Do you want to save changes" box appears and i
> > > can't for the life of me get the macro to select cancel.
> > > Here is the code i'm working on at the minute.
> > > Sub Auto_Close()
> > > '
> > > ' Auto_Close Macro
> > > ' Macro recorded 09/11/2007 by sparrett
> > > '
> > >
> > > Application.DisplayAlerts = False
> > > Dim Msg, Style, Title, Help, Ctxt, Response, MyString
> > > Msg = "Have You Saved A copy?"
> > > Style = vbYesNo + vbInformation
> > > Title = "Clear Down Button"
> > > Help = "DEMO.HLP" ' Define Help file.
> > > Ctxt = 1000 ' Define topic
> > > ' context.
> > > ' Display message.
> > > Response = MsgBox(Msg, Style, Title, Help, Ctxt)
> > > If Response = vbYes Then ' User chose Yes.
> > > MyString = "Yes" ' Perform some action
> > >
> > >
> > > Else ' User chose No.
> > > MyString = "No" ' Perform some action.
> > >
> > > Application.DisplayAlerts = False
> > > ThisWorkbook.Saved = False
> > >
> > >
> > > End If
> > > End Sub
> > >
> > > Any help would be appreciated.
> > >
> > > Cheers
> > >
> > > Simon

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I make prevent the Validate event from going off when the user hits the Close button (the X button) or selects "Close" from the system menu? 0to60 Microsoft Dot NET Framework Forms 5 10th Sep 2008 10:46 AM
Disable Close button and remove Close menu and then reactivate fpsoft Microsoft Dot NET Framework Forms 0 23rd Sep 2007 03:55 PM
Excel shoud not close all active books when clicking close button =?Utf-8?B?dGVjaG5vbWlrZQ==?= Microsoft Excel Misc 0 10th Jun 2005 05:35 PM
excel - Windows close button (x) should only close active workboo. =?Utf-8?B?Q29mZmVlQWRpY3Q=?= Microsoft Excel Setup 3 8th Feb 2005 04:30 AM
Close all programs / Shut Down windows does not auto close outlook =?Utf-8?B?TUtlcw==?= Microsoft Word Document Management 2 22nd Aug 2004 11:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:02 PM.