Peculiar excel event behaviour

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,892
Office Version
2016
Platform
Windows
Hi all.

I have this simple code in ThisWorkbook module that is supposed to hook the Apllication events so that the AppEvents_WorkbookBeforeClose event hadler is fired each time an open workbook is closed.

Code:
Option Explicit
 
Private WithEvents AppEvents As Application
 
Private Sub Workbook_Open()
 
    Set AppEvents = Application
    
    Call MyLoop
 
End Sub

Private Sub AppEvents_WorkbookBeforeClose _
(ByVal Wb As Workbook, Cancel As Boolean)
 
    MsgBox "Workbook " & Wb.Name & " closing..."
 
End Sub
 
Private Sub MyLoop()
 
    Do
        DoEvents
    Loop
 
End Sub
Now the problem I am having is that if I close a workbook, the BeforeClose event code fires as expected but if I close the main Excel application window, the routine never gets fired.

I know this is due to the background loop routine because when I remove the loop routine everything works as expected.

Can anybody shade some light as to why this is happening and perhaps provide a workaround .

Thanks .
 

Some videos you may like

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,892
Office Version
2016
Platform
Windows
Thanks Mike but that made no difference.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,397
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
This is a bit of a guess, but I would suspect that the abrupt termination of the loop codde causes a reset and your application variable to lose state.
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,892
Office Version
2016
Platform
Windows
This is a bit of a guess, but I would suspect that the abrupt termination of the loop codde causes a reset and your application variable to lose state.
Thanks Rory. That is what I thought .

I really need that loop so I am still trying to solve this problem. If I come to a solution i'll post it here.
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,381
Messages
5,510,953
Members
408,820
Latest member
MugsUpMan

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top