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):
- Create a new macro-enabled workbook.
- Record a new macro in the workbook. Stop the recording. Open VBA.
- Open the code for "Module 1" and replace the undesired code with the code below. Save the file.
- File -> Options -> Trust Center -> Trust Center Settings -> Macro Settings -> "Trust access to the VBA project object model" is selected. Save the file.
- I also have ensured
Application.EnableEvents = True
- 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: https://i.stack.imgur.com/yXkMK.png
Answer
That Workbook_SheetChange
code needs to be in the ThisWorkbook
code module, not in a regular module.
EDIT: and the Worksheet_SelectionChange
goes in the Worksheet code module
http://www.cpearson.com/excel/events.aspx
No comments:
Post a Comment