Before Close Event

G-Matriix

Board Regular
Joined
Sep 20, 2006
Messages
139
Hello All,

I am trying to ask to the user to check if they logged out when they close the workbook but my code is not working...

Here it is:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

Sheets("OD&D Log-in").Select
If Range("H5") = "reconcile" Then
    a = MsgBox("Do you want to Log-Out?", _
        vbYesNo)
If a = vbNo Then Cancel = True
If a = vbYes Then
Sheets("OD&D Log-in").Select
Else
Workbooks("Daily OSD Log (ver5).xls").Close SaveChanges = True

End Sub

Any Ideas??

Thanks
 
Last edited by a moderator:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

DonkeyOte

MrExcel MVP
Joined
Sep 6, 2002
Messages
9,124
Not entirely sure what you want to do as the initial code did not make sense (if answer = yes to log out prompt) however below will prompt - if yes then file will save & close
(Note: initial code had savechanges = true -- this will not save, need savechanges:=true to save)

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("OD&D Log-in").Select
If Range("H5") = "reconcile" Then
    a = MsgBox("Do you want to Log-Out?", vbYesNo)
    Select Case a
        Case vbNo
            Cancel = True
        Case Else
            Workbooks("Daily OSD Log (ver5).xls").Close SaveChanges:=True
    End Select
End If
End Sub
 
Upvote 0

G-Matriix

Board Regular
Joined
Sep 20, 2006
Messages
139
Not entirely sure what you want to do as the initial code did not make sense (if answer = yes to log out prompt) however below will prompt - if yes then file will save & close
(Note: initial code had savechanges = true -- this will not save, need savechanges:=true to save)

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("OD&D Log-in").Select
If Range("H5") = "reconcile" Then
    a = MsgBox("Do you want to Log-Out?", vbYesNo)
    Select Case a
        Case vbNo
            Cancel = True
        Case Else
            Workbooks("Daily OSD Log (ver5).xls").Close SaveChanges:=True
    End Select
End If
End Sub


Hey thanks,

However, when I close the document it doesn't prompt for the Msgbox...???? When I hit the X to close it should run ......right??????
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Don't know if it will help but I would suggest you reference the worksheet in the If statement.
Code:
If Sheets("OD&D Log-in").Range("H5") = "reconcile" Then
If you don't then VBA will be looking at H5 on whatever it considers the active sheet.
 
Upvote 0

G-Matriix

Board Regular
Joined
Sep 20, 2006
Messages
139
Don't know if it will help but I would suggest you reference the worksheet in the If statement.
Code:
If Sheets("OD&D Log-in").Range("H5") = "reconcile" Then
If you don't then VBA will be looking at H5 on whatever it considers the active sheet.

Thanks....I don't know what I am doing wrong...it will not prompt....when closed....there is got to be something I am leaving out...

Any Ideas???
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Where is the code located?

It should be in the ThisWorkbook module.

What result do you get when you open the Immediate Window (CTRL+G) and enter this?
Code:
?Application.EnableEvents
If that returns False then it means that at some point, probably by accident, events have been disabled.

To enable them enter this in the Immediate Window.
Code:
Application.EnableEvents = True
 
Upvote 0

G-Matriix

Board Regular
Joined
Sep 20, 2006
Messages
139
Where is the code located?

It should be in the ThisWorkbook module.

What result do you get when you open the Immediate Window (CTRL+G) and enter this?
Code:
?Application.EnableEvents
If that returns False then it means that at some point, probably by accident, events have been disabled.

To enable them enter this in the Immediate Window.
Code:
Application.EnableEvents = True

It returned True .....and the code is located in one of the modules

???????? I am puzzled???
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Which module is the code in?
 
Upvote 0

Forum statistics

Threads
1,191,671
Messages
5,987,957
Members
440,121
Latest member
eravella

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top