VBAmacros崩溃

我尝试创build一个macros,但是当它启动时,excel不处理它并崩溃。

这是macros:

Private Sub Worksheet_Change(ByVal Target As Range) Dim i As Integer Dim sous As Integer If Not IsEmpty(Cells(4, 2)) And IsNumeric(Cells(4, 2).Value) And Not IsEmpty(Cells(4, 3)) And IsNumeric(Cells(4, 3).Value) Then For i = Cells(4, 3).Value To Cells(4, 2).Value sous = i - Cells(4, 3).Value Cells(5 + sous, 4).Value = i Next i MsgBox "Yataaaah" End If End Sub 

问题是行Cells(5 + sous, 4).Value = i因为如果我把它放在评论,它的工作。

任何人有任何想法,为什么Excel不处理它?

非常感谢你。 Ps:我在Mac上,我正在使用Excel 15.19.1

你必须closures事件。 Excel崩溃的原因是select更改事件更改了一个触发select更改事件的值。 它陷入了一个无限循环。

 Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False Dim i As Integer Dim sous As Integer If Not IsEmpty(Cells(4, 2)) And IsNumeric(Cells(4, 2).Value) And Not IsEmpty(Cells(4, 3)) And IsNumeric(Cells(4, 3).Value) Then For i = Cells(4, 3).Value To Cells(4, 2).Value sous = i - Cells(4, 3).Value Cells(5 + sous, 4).Value = i Next i MsgBox "Yataaaah" End If Application.EnableEvents = True End Sub