Share via

worksheet_change not working

Anonymous
2023-01-06T21:52:43+00:00

i have the following macro wich does not work

Private Sub Worksheet_Change(ByVal Target As Range)

'Declare variables

Dim sourceSheet As Worksheet

Dim sourceRange As Range

'Set variables to sheets and ranges

Set sourceSheet = ThisWorkbook.Sheets("sheet1")

Set sourceRange = sourceSheet.Range("D16")

'Check if the value in cell D15 has changed

If Target.Address = "$D$15" Then

'Check if cell D15 contains the value 9 

If Target.Value = 9 Then 

    'Replace the value in cell D16 with 7 

    sourceRange.Value = 7 

End If 

End If

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2023-01-06T22:06:55+00:00

Are you sure that the code is in the worksheet module of the sheet containing the cell D15 that you want to monitor?

The worksheet module can be activated by right-clicking the sheet tab and selecting 'View Code' from the context menu.

Also make sure that macros are allowed.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-01-06T22:30:13+00:00

    That worked:

    The code was in a different folder

    Was this answer helpful?

    0 comments No comments