How to run Excel VBA code upon change in cell value

LorenHartley-7553 0 Reputation points
2024-01-20T19:22:33.1033333+00:00

I would like to execute my first Excel VBA code whenever selected cell values change. The Sub MaxXfer() works fine manually. I now would like it to execute when certain cell values change. Encountering "End If without block If" error.

Private Sub Worksheet_Change(ByVal Target As Range) Dim KeyCells As Range

' The variable KeyCells contains the cells that will ' cause an alert when they are changed. Set KeyCells = Range("I12:O12")

If Not Application.Intersect(KeyCells, Range(Target.Address)) _ Is Nothing Then

    ' Place your code here.
```Sub MaxXfer()
        Dim k As Long
        Dim ResultCell As Range
        Dim ChangingCell As Range
        Dim TargetScore As Integer
    
TargetScore = -2000

For k = 9 To 15 Set ResultCell = Cells(65, k) Set ChangingCell = Cells(13, k) ResultCell.GoalSeek TargetScore, ChangingCell Next k

End Sub
Microsoft 365 and Office Development Other
Microsoft 365 and Office Excel For business Windows
Developer technologies VB
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tom van Stiphout 1,861 Reputation points MVP Volunteer Moderator
    2024-01-20T20:18:26.1866667+00:00

    The code is hard to read but it appears you have a procedure MaxXfer within another procedure Worksheet_Change. That is not allowed.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.