当我运行此代码时,Excel工作簿会挂起一两秒钟... 该怎么办?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim a As Variant
Dim b As Variant
Dim Number_of_Sims As Integer
Number_of_Sims = 76
For i = 3 To Number_of_Sims
If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub 'don't run unless change in column A
Application.EnableEvents = False 'stop executing this code until we are done
If Len(Range("s10").Value) = 0 Then
a = Cells(i, 23).Value
Cells(9, 19).Value = a
b = Cells(16, 19).Value
Cells(i, 24).Value = b
End If
Next
Application.EnableEvents = True
End Sub
答案 0 :(得分:2)
首先,你需要释放不依赖于循环的东西的循环:
Application.EnableEvents = False
If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub
然后你的a
和b
似乎毫无用处,所以我只收集了两行!
所以试一试,这应该更有效率:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub 'don't run unless change in column A
With Application
.EnableEvents = False 'stop executing this code until we are done
.DisplayAlerts = False
.ScreenUpdating = False
'.Calculation = xlCalculationManual
End With
Dim Number_of_Sims As Integer
Number_of_Sims = 76
If Len(Range("s10").Value) <> 0 Then
Else
For i = 3 To Number_of_Sims
Cells(9, 19).Value = Cells(i, 23).Value
Cells(i, 24).Value = Cells(16, 19).Value
Next i
End If
With Application
.EnableEvents = True
.DisplayAlerts = True
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub