selectCase语句就是将Application.ScreenUpdating转回

在运行这个子文件时,我不断收到Screen.Updating。 缩小到这个Select Case语句。 代码的作品,只是不断重新开始更新。

If count > 1 Then Select Case count Case 2 Range("N10") = arrSkills(1) Case 3 Range("N10") = arrSkills(1) Range("N11") = arrSkills(2) Case 4 Range("N10") = arrSkills(1) & " " & arrSkills(2) Range("N11") = arrSkills(3) Case 5 Range("N10") = arrSkills(1) & " " & arrSkills(2) Range("N11") = arrSkills(3) & " " & arrSkills(4) Case Else MsgBox "Make room for more Skills" End Select Else End If 

这是Worksheet_Change事件

 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Cells.count > 1 Or IsEmpty(Target) Then Exit Sub If Not Intersect(Me.Range("Table[Name]"), Target) Is Nothing Then Application.ScreenUpdating = False 'Code where the Select Case Statement is called End If Application.ScreenUpdating = True End Sub 

发生什么事情是,您对单元格N10和N11的更改将重新触发Worksheet_Change事件,并且该事件的最后一行是设置Application.ScreenUpdating = True

通常最好是在处理事件时禁用事件(除非你确实需要因为某种原因recursion调用它)。 禁用事件是通过使用

 Application.EnableEvents = False 

注意:当你完成时,要非常小心地重新启用事件,否则你会花上几个小时,想知道为什么Change (etc)事件不再被触发。

所以你的代码会写得更好:

 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Cells.count > 1 Or IsEmpty(Target) Then Exit Sub If Intersect(Me.Range("Table[Name]"), Target) Is Nothing Then Exit Sub Application.ScreenUpdating = False 'On Error statements are often more trouble than they are worth - but this is one ' occasion where it is dangerous not to use one. You can't afford not to go ' through the statement re-enabling events. On Error GoTo ReEnableEvents Application.EnableEvents = False '... If count > 1 Then Select Case count Case 2 Range("N10") = arrSkills(1) Case 3 Range("N10") = arrSkills(1) Range("N11") = arrSkills(2) Case 4 Range("N10") = arrSkills(1) & " " & arrSkills(2) Range("N11") = arrSkills(3) Case 5 Range("N10") = arrSkills(1) & " " & arrSkills(2) Range("N11") = arrSkills(3) & " " & arrSkills(4) Case Else MsgBox "Make room for more Skills" End Select Else '... End If '... Application.ScreenUpdating = True ReEnableEvents: Application.EnableEvents = True End Sub