I am having a problem with Excel crashing, when I run VBA code on an excel sheet.
I’m trying to add the following formula on worksheet change:
Private Sub Worksheet_Change(ByVal Target As Range)
Worksheets("testpage").Range("A1:A8").Formula = "=B1+C1"
End Sub
When this code is run i get a message saying “excel has encountered a problem and needs to close” and excel closes.
If I run the code in the Worksheet_Activate() procedure, it works fine and doesn’t crash
Private Sub Worksheet_Activate()
Worksheets("testpage").Range("A1:A8").Formula = "=B1+C1"
End Sub
But I really need it to work in the Worksheet_Change() procedure.
Has anyone experienced similar crashes when using the Worksheet_Change() event and can anyone point in the right direction to fix this issue ?

I recommend this when using
Worksheet_ChangeYou do not need the sheet name. In a Sheet Code Module, an unqualified Range reference refers to that sheet. That said, it is clearer to use the
Mequalifier. If you are trying to use another sheet, then qualify the range reference with that sheet.Whenever you are working with
Worksheet_Changeevent, always switchOffevents if you are writing data to any cell. This is required so that the code doesn’t retrigger the Change event, and go into a possible endless loopWhenever you are switching off events, use error handling to turn it back on, else if you get an error, the code will not run the next time.
Try this
Few other things that you may want to know when working with this event.
If you want to ensure that the code doesn’t run when more than one cell is changed then add a small check
The
CountLargewas introduced in Excel 2007 onward becauseTarget.Cells.Countreturns anLongvalue which can error out in Excel 2007 becuase of increased total cells count.To work with all the cells that were changed use this code
To detect change in a particular cell, use
Intersect. For example, if a change happens in CellA1, then the below code will fireTo detect change in a particular set of range, use
Intersectagain. For example, if a change happens in rangeA1:A10, then the below code will fireNote: If you were getting an error earlier and you made the above changes and If your code is still not working then it is possible that the events have not been reset. In the
Immediate Window, typeApplication.EnableEvents = Trueand press the ENTER key. This will reset it toTrue. If you do not see theImmediate Window, the press the shortcut key Ctl+G to launch theImmediate Window.