Handling Before Close events in Excel 2003

Img

New Member
Joined
Jan 7, 2009
Messages
7
Hi there
I'm going mad!

I have a working script to capture the user name and date of modification before close. The script is saved as a event headed : Private Sub Workbook_BeforeClose(Cancel As Boolean) in the VB script Editor under This WorkBook.
It works perfectly when you open a fresh instance of Excel.

After the workbook is closed and if I reopen the same workbook again (without quitting Excel), it does not execute the code unless I quit Excel and relaunch it again. So I can get round this with Application.Quit

However, I cannot ensure that users will always open this workbook in a fresh instance of Excel. They could be openning this workbook after working on other workbooks with some other Before Close routines in the same instance of Excel.

Is this a bug in Excel? Can anyone help to find a way round this?

Thank You
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Welcome to the Board!

Are you by chance disabling events (Application.EnableEvents = False) in your code and not turning them back on?
 
Upvote 0
Thank You Smitty

This is the code I used. Could you, please, please take a look if anything looks odd?? that can cause it?

Really appreciate this. I spent hours trouble shooting but found no answers.

Thank You again !

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
UserName = Environ("USERNAME")
 
Dim XCount As Integer
XCount = Application.Sheets.Count
Revised = Format(Now(), "dd mmmm yyyy")
 
Dim i As Integer
Dim Str As String
i = 1
Str = Sheets(i).Name
 
Dim Msg, Style, Title, Response
Msg = "Is this a revision from the previous version? If Yes, Last Revised Date will be changed to today's date." ' Define message.
Style = vbYesNo
Title = "Last Revision" ' Define title.
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then ' User chose Yes.
Sheets(Str).Range("C3").Value = Revised
Sheets(Str).Range("F3").Value = UserName
MsgBox (UserName & ", " & "The Last Revised Date is now changed to " & Revised)
ActiveWorkbook.Save
 
If XCount > 1 Then
 
For i = i To XCount
Str = Sheets(i).Name
Sheets(Str).Range("C3").Value = Revised
Sheets(Str).Range("F3").Value = UserName
Next i
 
 
ActiveWorkbook.Save
End If
Else
MsgBox ("GoodBye")
End If
Application.Quit
 
End Sub
 
Last edited by a moderator:
Upvote 0
How is the workbook being closed? Is the user closing it via the Excel interface, or are you closing it programmatically via code? In other words, please can you describe what action is raising the Workbook_BeforeClose event handler?

Colin
 
Upvote 0
I am really grateful for folks like you people on this Board! :)

User is closing it via Excel Interface.

Thank You Colin.
 
Upvote 0
This seems to be working fine for me. Note that you should avoid using reserved words like UserName as variables.

<font face=Tahoma><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_BeforeClose(Cancel <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>)<br>    <SPAN style="color:#00007F">Dim</SPAN> XCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> CurrentUser <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, Revised <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, Str <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> Msg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, Style <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, Title <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, Response <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>        XCount = Application.Sheets.Count<br>        CurrentUser = Environ("USERNAME")<br>        Revised = Format(Now(), "dd mmmm yyyy")<br>        i = 1<br>        Str = Sheets(i).Name<br> <br>            Msg = "Is this a revision from the previous version? If Yes, Last Revised Date will be changed to today's date." <SPAN style="color:#007F00">' Define message.</SPAN><br>            Style = vbYesNo<br>            Title = "Last Revision" <SPAN style="color:#007F00">' Define title.</SPAN><br>            Response = MsgBox(Msg, Style, Title)<br><br>                <SPAN style="color:#00007F">If</SPAN> Response = vbYes <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#007F00">' User chose Yes.</SPAN><br>                    <SPAN style="color:#00007F">With</SPAN> Sheets(Str)<br>                        .Range("C3").Value = Revised<br>                        .Range("F3").Value = CurrentUser<br>                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <br>                MsgBox (CurrentUser & ", " & "The Last Revised Date is now changed to " & Revised)<br>                <br>                ActiveWorkbook.Save<br> <br>                <SPAN style="color:#00007F">If</SPAN> XCount > 1 <SPAN style="color:#00007F">Then</SPAN><br>                    <SPAN style="color:#00007F">For</SPAN> i = i <SPAN style="color:#00007F">To</SPAN> XCount<br>                        Str = Sheets(i).Name<br>                            <SPAN style="color:#00007F">With</SPAN> Sheets(Str)<br>                                .Range("C3").Value = Revised<br>                                .Range("F3").Value = CurrentUser<br>                            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>                    <SPAN style="color:#00007F">Next</SPAN> i<br>                    ActiveWorkbook.Save<br>                <SPAN style="color:#00007F">Else</SPAN><br>                    MsgBox ("GoodBye")<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br>               Application.Quit<br> <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Smitty,
YOU ARE A STAR!!
It works and didn't give me the problems.
Still can't figure out what was causing mine to behave the way it did.

But Very grateful for your time and advice!
THANK YOU!

Have a fantastic day! (ps can we have your Californian sunshine here instead of this cold rainny and dull weather? :cool:)
 
Upvote 0
(ps can we have your Californian sunshine here instead of this cold rainny and dull weather? :cool:)

I suppose...It was 32 and sunny when I left home this morning. :) Supposed to get into the 60's today. But we really need the rain!
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,816
Members
449,095
Latest member
m_smith_solihull

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
Back
Top