简体   繁体   中英

Changing value of a cell to trigger a macro

As the title suggests, I want a specific macro (PopulateCalculatorWithWeekChosen) to run whenever a user changes the value in the 'Summary' worksheet, cell H10.

I found the below question and answer and tried it but it doesn't seem to trigger it. Did I understand correctly that I put the code below...

Trigger event when select from dropdown

Sub PopulateCalculatorWithWeekChosen()

If Target.Address = "$H$10" Then
With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With

[the rest of the code in the PopulateCalculatorWithWeekChosen macro]

    With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With
End If
End Sub

...in the right place? It doesn't seem to do anything. Thanks in advance for any help.

The problem is you've defined a macro and the question/answer you reference is using the Worksheet_Change event. In your VBA code, you should have Worksheet object which has a Change event. An event is just a function that is triggered when a specific thing happens. In this case, the function Worksheet_Change triggers whenever the worksheet is modified. You should have something like this:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$H$10" Then

      With Application
        .EnableEvents = False
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
      End With

      With Application
        .EnableEvents = True
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
      End With

    End If

End Sub

When you modify the worksheet, Excel will automatically call this function and tell you which cells were modified by providing that information in the Target parameter.

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