Consulting

Results 1 to 3 of 3

Thread: Excel Worksheet_SelectionChange event not firing at all? (on both Office 2013 & 2016)

  1. #1

    Question Excel Worksheet_SelectionChange event not firing at all? (on both Office 2013 & 2016)

    I've been having a heck of a time accomplishing what I thought would be an incredibly simple test. All I am trying to achieve is to pop up a MsgBox when a user selects a new cell or changes the contents of a cell.

    I've been at this for about 6 hours and so far have zero success! I have identical behavior with Office 2016 (Windows 10) and with Office 2013 (Windows 7).

    Here are my method(s):

    1. Create a new macro-enabled workbook.
    2. Record a new macro in the workbook. Stop the recording. Open VBA.
    3. Open the code for "Module 1" and replace the undesired code with the code below. Save the file.
    4. File -> Options -> Trust Center -> Trust Center Settings -> Macro Settings -> "Trust access to the VBA project object model" is selected. Save the file.
    5. I also have ensured Application.EnableEvents = True
    6. I am expecting to be able to click on various cells, or edit cells, and received a MsgBox whenever the event occurs.


    Here is my code:
    Option Explicit 
     
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)       
        MsgBox "changed!"   
    End
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        MsgBox "selected!" 
    End Sub
     
    Public Sub Just_In_Case() 
        Application.EnableEvents = True 
    End Sub

    What am I missing? Is there a security setting preventing this action event? I have the same behavior online at work as I do offline at home.


    Thank you in advance for your help!

    PS Here is the screenshot of my VBA environment, if relevant:
    Capture.jpg

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    You have the code in a Standard Module


    This is a Workbook event and goes in the ThisWorkbook module and applies to all changes in all worksheets


    Option Explicit
    
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        MsgBox "worksheet changed! " & Sh.Name
    End Sub
    Capture2.JPG

    This is a worksheet event and goes in the module of the worksheet to which it applies. It only applies to that worksheet

    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        MsgBox "selected " & Target.Address
    End Sub
    Capture.JPG
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    Quote Originally Posted by Paul_Hossler View Post
    You have the code in a Standard Module

    This is a Workbook event and goes in the ThisWorkbook module and applies to all changes in all worksheets
    That did it! I thank you, kind sir!

Tags for this Thread

Posting Permissions

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