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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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:
Upvote 0
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:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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