Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 14th, 2019, 08:54 AM
Registered User
 
Join Date: Feb 2017
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
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.
 
Old June 14th, 2019, 03:13 PM
Registered User
 
Join Date: Feb 2017
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.