Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 14th, 2019, 08:54 AM
Registered User
Points: 21, Level: 1
Points: 21, Level: 1 Points: 21, Level: 1 Points: 21, Level: 1
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Feb 2017
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Red face VBA Close Code not closing properly

I am trying to close an application when the user clicks either the Red X, a button. Previously I used code below to close the application, when the button was clicked. It worked as expected. Here is the code:
Code:
Sub Exit_Referrals()
Dim MsgBoxResult As Long

 MsgBoxResult = MsgBox("Would you like to Exit the Referral Workbook?" & vbCr, _
 vbYesNo, "Voc. Rehab. - Referral")
 If MsgBoxResult = vbNo Then
 Exit Sub
 ElseIf MsgBoxResult = vbYes Then
 Sheets("TOC").Select
 Application.Calculation = xlCalculationAutomatic
Application.Quit
ThisWorkbook.Close SaveChanges:=True
 End If
End Sub
When i tried to activate the code, when the Red X, was clicked, I ran into some problems. 1) You are asked if you want to exit the application 2x; 2) The application closes, but Excel remains open; 3) if you choose No (remain open),the application closes. I have the same problem when the button is clicked. Here is the current code:
This is located on thisworkbook
Code:
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
      'Exits application
       Call Exit_Referrals
End Sub
The Sub Exit_Referrals() is located in module 1. I know I am missing something, but it escapes me.
Reply With Quote
  #2 (permalink)  
Old June 14th, 2019, 03:13 PM
Registered User
Points: 21, Level: 1
Points: 21, Level: 1 Points: 21, Level: 1 Points: 21, Level: 1
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Feb 2017
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Cool Vba code not closing properly - Solved

I could not believe how simple the solution was. I even added a msgbox. Here is the first code & placed in THEWORKBOOK:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If CloseMode = vbFormControlMenu Then
    Cancel = True
    MsgBox "Click on Exit button to close!"
    Application.EnableEvents = False
End If
    
End Sub
The second code is in the module 1.
Code:
Sub Exit_Referrals()
Dim MsgBoxResult As Long
  MsgBoxResult = MsgBox("Would you like to Exit the Referral Workbook?" & vbCr, _
   vbYesNo, "Vocational Services Database - " & ActiveSheet.Name)
   If MsgBoxResult = vbNo Then
    Exit Sub
 ElseIf MsgBoxResult = vbYes Then
   Sheets("TOC").Select
    Application.Calculation = xlCalculationAutomatic
      If ThisWorkbook.Saved = False Then
            ThisWorkbook.Save
            End If
             ThisWorkbook.Close SaveChanges:=True
  Application.Quit
 End If
 
End Sub
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Properly closing a file Gary108 BOOK: Visual Basic 2012 Programmer's Reference 1 May 20th, 2013 11:16 AM
Clearing Sessions - Browser Closing using close(cross mark at top right corner) butt abinashpatra ASP.NET 2.0 Professional 7 January 11th, 2009 11:14 PM
Close Child window automatically on closing parent vikas67k C# 1 August 26th, 2008 02:00 AM
make outlook.exe process close properly dgr7 Beginning VB 6 3 April 18th, 2007 03:51 PM
I can't properly install code on VS cybermia BOOK: ASP.NET Website Programming Problem-Design-Solution 5 March 23rd, 2005 02:42 PM



All times are GMT -4. The time now is 04:00 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.