VBA number of events running

Rmv

New Member
Joined
Jul 25, 2010
Messages
1
Hi all! New to the forum.

I'm using the change event in a worksheet and when I initiate a change, it runs the code as expected. My code makes additional changes which triggers the change event code to run again since I leave application.events = true.

My question is how can I keep track whether I am in the original change event or if I'm in one of the sub change events which were triggered by the main one.

Thanks!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

crimson_b1ade

Well-known Member
Joined
Sep 27, 2008
Messages
1,557
I'm not sure I understand the question but you could add a MsgBox to the top of your procedures. That way you'll get a popup box on which code is running. Is this for debugging purposes? Example:

Code:
Sub SomeProcedure()
 
Msgbox "Now Running SomeProcedure"
 
your other code goes here
  
End Sub
 
Last edited:

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,875
Office Version
  1. 2019
  2. 2016
  3. 2010
Platform
  1. Windows
Hi & Welcome to the Board!

This is my understanding of issue with its solution, see details in the code's comments:
Rich (BB code):

' Code of the sheet's module

Dim EventIsCharged As Boolean

Private Sub Worksheet_Change(ByVal Target As Range)
  
  ' This is the key to skip the code replaying
  If EventIsCharged Then Exit Sub
  
  ' Disable events triggering
  Application.EnableEvents = False
  
  ' Set flag to not trigger of this subrotine again till the end of the code
  EventIsCharged = True
  
  ' Safe calling of some subroutines even with possible Application.EnableEvents = True
  MsgBox "Test() is calling"
  Call Test
  
  ' Reset the flag to allow triggering of this subroutine
  EventIsCharged = False
  
  ' Restore events
  Application.EnableEvents = True
  
  
End Sub

Sub Test()
  
  ' Let's assume that for some reason the events should be enabled
  Application.EnableEvents = True
  
  ' Do something with cells.
  Cells(1, 1).Formula = Cells(1, 1).Formula
  
  ' If EventIsCharged = True then the 1st line of Worksheet_Change() code will exit that sub
    
End Sub
Regards
 
Last edited:

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,205
Perhaps something like
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Static countOfChange As Long
    countOfChange = countOfChange + 1
    
    Rem your code
    
    countOfChange = countOfChange - 1
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,612
Messages
5,832,698
Members
430,154
Latest member
Froggy16

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