VBA无限循环

有没有一种构造可以保护VBA函数在进入无限循环时不会使Excel崩溃? 这不是什么大不了的事,只是在运行之前忘了保存最新的更改而不方便。

在更一般的说明中,当然在这种情况下,保持重启button不能超越MS的function。 这里发生的是屏幕变白,唯一的出路似乎是打开任务pipe理器并closures应用程序。

你好,这是我的第一个答复/职位。 基本上我在程序开始时设置了开始时间,然后在循环内与now()比较。 如果差值大于某个值( 0.0001〜10秒),则要求用户中断程序或继续(并将“ 开始 ”variables重置为now() )。

这个特定的过程遍历范围内的所有非空白单元格,并将数字值格式化为千分隔。

我希望这会对其他人有所帮助:

 Sub someVBA() Dim rng As Range, Cell As Range Dim yn As String Dim start As Double 'this VBA code formats all numeric cells as #, ##0 start = Now() Set rng = ActiveSheet.Columns("A:FZ").SpecialCells(xlCellTypeConstants) For Each Cell In rng If IsNumeric(Cell.Value) Then Cell.NumberFormat = "#,##0" 'if the difference between start and now is > approx. 10 seconds If Now() - start > 0.0001 Then roz = MsgBox("You probably stuck in a loop. Press YES to exit, otherwise you will continue", vbInformation + vbYesNo) If roz = vbYes Then Exit For Else start = Now() End If End If Next Cell End Sub 

最近我确认了这个解决scheme

即使循环中缺lessDoEvents语句,仍然可以中断过程事件:

按住Esc键,同时用鼠标点击 任务栏图标 。 这样你就可以强制Excel获得焦点并收听键盘。