简体   繁体   中英

VBA Worksheet_SelectionChange not working

My VBA is a bit rusty so I am having trouble building an application that automatically updates some cells when the value of a cell is modified.

I've started with this basic subroutine, located on a Worksheet page:

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = True
If Not Intersect(Target, Target.Worksheet.Range("A1")) Is Nothing Then
    MsgBox "Lorem Ipsum"
End If

End Sub

This subroutine should pop-up a message box Lorem Ipsum when cell A1's value is modified.

However this is not working. I've been unable to find where is the issue; typical problems (subroutine located in a module, EnableEvents turned False ) should not happen with the code above; I've also tried to change Target.Worksheet.Range("A1") to Range("A1") .

Does anyone know where is the issue coming from?

The problem is that when Application.EnableEvents are off, the Worksheet Events don't fire and your code will not get a chance to run.

To turn it back on, type Application.EnableEvents = True in the Immediate Window as shown below

在此处输入图片说明

Now your code should run without any problems.

I copied your code and it worked as one should expect. Note though that you need to manually edit cell A1 on the worksheet where the code is to get up the MsgBox. Recalculation has no bite on the worksheet change event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM