Consulting

Results 1 to 10 of 10

Thread: Need Help Understanding Events

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Need Help Understanding Events

    If I implement Application.EnableEvents = False at the beginning of the Worksheet_Change sub and Application.EnableEvents = True at the end of the sub, and then I proceed to change the value of a cell within that particular worksheet, should a Worksheet_SelectionChange event be triggered once Events are turned back on to account for the fact that the cursor position moved after the Target value was changed?

    For example, if I create a new blank workbook and paste the following code in one of the Sheet modules, a SelectionChange event seems to get called every time, regardless whether Events are turned off, and regardless of whether I create a global Boolean variable and set it accordingly.

    Option Explicit
    Public testing As Boolean
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False: testing = True
    
    
    'Change the value of a cell in the worksheet
    
    
    Application.EnableEvents = True: testing = False
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If testing = True Then Exit Sub
    MsgBox "selection changed"
    
    
    End Sub

    Could someone please explain whether I'm trying to do the impossible?

    P.S. I'm running Excel 2007.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    Option Explicit
    Public testing As Boolean
     
    Private Sub Worksheet_Change(ByVal Target As Range)
        Application.EnableEvents = False: testing = True
         'Change the value of a cell in the worksheet
    End Sub
     
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Application.EnableEvents = True: testing = True
        If testing = True Then MsgBox "selection changed"
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by mdmackillop View Post
    Try
    Option Explicit
    Public testing As Boolean
     
    Private Sub Worksheet_Change(ByVal Target As Range)
        Application.EnableEvents = False: testing = True
         'Change the value of a cell in the worksheet
    End Sub
     
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Application.EnableEvents = True: testing = True
        If testing = True Then MsgBox "selection changed"
    End Sub
    Thanks! Just out of curiosity, why does "If testing = true then Exit Sub" not accomplish the same thing? I was under the understanding that such a line would exit the sub before any subsequent line of code had a chance to be executed?
    Last edited by Opv; 07-13-2017 at 01:29 PM.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Nothing happens after the Exit Sub except the MsgBox. I removed it to see if the macro was triggered.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by mdmackillop View Post
    Nothing happens after the Exit Sub except the MsgBox. I removed it to see if the macro was triggered.
    I am misunderstanding you. Are you saying that after Exit Sub, the Msgbox popup is NOT running or that it is running? For me, after the Exit Sub statement, any and all of the following lines of code seem to be running. What I am wanting to accomplish is to ignore all code following the Exit Sub statement if either Events are turned off or the "testing" boolean statement is = true. I can't seem to accomplish that for some reason.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I would not think of events that way

    Say for example, if you changed B4 then you also want to change C4 to be 2x that

    If you only had this ...

    Option Explicit 
    
    Private Sub Worksheet_Change(ByVal Target As Range) 
    
    If Target.Address = "$B$4" then
        Range("C4").Value = 2 * Range("B4").value
    Endif
    
    End Sub

    ... when you change the value in B4, the event will fire, BUT it will fire again because the event handler changed C4

    Turning off event handling will allow C4 to be changed and not trigger a circular event chain

    Option Explicit 
    
    Private Sub Worksheet_Change(ByVal Target As Range) 
    
    If Target.Address = "$B$4" then
        Application.EnableEvents = False
        Range("C4").Value = 2 * Range("B4").value
        Application.EnableEvents = True
    Endif
    
    End Sub

    This fires when the selection changes

    Option Explicit 
    Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
        MsgBox "selection changed to " & Target.Address
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thanks for the help. I realize that my initial example is overly broad. That said, are there no instances in which a complete disabling of the SelectionChange sub might be appropriate?

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I can only recall one use I had for SelectionChange; preventing users from accessing Rows 1:4 to change things. I can't see a reason to consider it in conjunction with another macro. It can obviously be prevented from running after another macro by leaving the focus on the Selection area of that macro.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thanks for the help.

  10. #10
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    I think figured out at least part of what I initially found so confusing, and why part of my initial reasoning resulted in such a broad approach. What I was really curious about is if and to what extent events triggered in WorksheetChange effect events triggered in the SelectionChange sub. What I really needed to understand is how to capture the previously edited range so that I could trigger (or not trigger) a particular SelectionChange event. Once I capture the previously edited range and configure SelectionChange to trigger/not trigger accordingly, everything seems to work as desired. Thanks for helping me think through this process.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •