Worksheet_Change 次慢速自动计算
Worksheet_Change Sub Slow Autocalculation
我有一个非常复杂的 excel 文件,很久以前就应该用数据库替换它,但是当时不可能。需要实现一种方法来记录对特定列进行更改的时间并记录进行更改的日期。
我写了以下脚本:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim VRange As Range, cell As Range
Dim Vrange2 As Range, cell2 As Range
Dim Vrange3 As Range, Cell3 As Range
Dim Vrange4 As Range, Cell4 As Range
Dim Vrange5 As Range, Cell5 As Range
Dim Vrange6 As Range, Cell6 As Range
Application.Calculation = xlCalculationManual
Set VRange = Range("J5:J7000")
For Each cell In Target
If Union(cell, VRange).Address = VRange.Address Then
cell.Offset(, 8) = "TS on " & Date
End If
Next cell
Set Vrange2 = Range("K5:K7000")
For Each cell2 In Target
If Union(cell2, Vrange2).Address = Vrange2.Address Then
cell2.Offset(, 7) = "GS on " & Date
End If
Next cell2
Set Vrange3 = Range("M5:M7000")
For Each Cell3 In Target
If Union(Cell3, Vrange3).Address = Vrange3.Address Then
Cell3.Offset(, 5) = "P on " & Date
End If
Next Cell3
Set Vrange4 = Range("O5:O7000")
For Each Cell4 In Target
If Union(Cell4, Vrange4).Address = Vrange4.Address Then
Cell4.Offset(, 3) = "GD on " & Date
End If
Next Cell4
Set Vrange5 = Range("P5:P7000")
For Each Cell5 In Target
If Union(Cell5, Vrange5).Address = Vrange5.Address Then
Cell5.Offset(, 2) = "TD on " & Date
End If
Next Cell5
Application.Calculation = xlCalculationAutomatic 'doesnt do anything
End Sub
它像发条一样工作,但是由于电子表格的性质,如果更改了多个单元格,则需要永远重新计算(因为它会在单元格更改后重新计算,然后运行宏,这会更改另一个单元格,因此它会再次重新计算并为每个更改的单元格重复该过程)当您更改超过 50 个单元格时,1.5 秒的计算时间变得难以忍受。
插入 Application.Calculation=xlCalculationManual
没有帮助,因为我需要它在宏完成后自动插入 运行,所以我必须把 Application.Calculation=xlCalculationAutomatic
放在最后。
我在这里不知所措,非常感谢任何帮助。
在处理 Worksheet_Change
事件以及 Calculation
模式更改时,始终使用:
Application.EnableEvents = False
当所有单元格都改变后,使用相反的方法再次启用它:
Application.EnableEvents = True
我有一个非常复杂的 excel 文件,很久以前就应该用数据库替换它,但是当时不可能。需要实现一种方法来记录对特定列进行更改的时间并记录进行更改的日期。
我写了以下脚本:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim VRange As Range, cell As Range
Dim Vrange2 As Range, cell2 As Range
Dim Vrange3 As Range, Cell3 As Range
Dim Vrange4 As Range, Cell4 As Range
Dim Vrange5 As Range, Cell5 As Range
Dim Vrange6 As Range, Cell6 As Range
Application.Calculation = xlCalculationManual
Set VRange = Range("J5:J7000")
For Each cell In Target
If Union(cell, VRange).Address = VRange.Address Then
cell.Offset(, 8) = "TS on " & Date
End If
Next cell
Set Vrange2 = Range("K5:K7000")
For Each cell2 In Target
If Union(cell2, Vrange2).Address = Vrange2.Address Then
cell2.Offset(, 7) = "GS on " & Date
End If
Next cell2
Set Vrange3 = Range("M5:M7000")
For Each Cell3 In Target
If Union(Cell3, Vrange3).Address = Vrange3.Address Then
Cell3.Offset(, 5) = "P on " & Date
End If
Next Cell3
Set Vrange4 = Range("O5:O7000")
For Each Cell4 In Target
If Union(Cell4, Vrange4).Address = Vrange4.Address Then
Cell4.Offset(, 3) = "GD on " & Date
End If
Next Cell4
Set Vrange5 = Range("P5:P7000")
For Each Cell5 In Target
If Union(Cell5, Vrange5).Address = Vrange5.Address Then
Cell5.Offset(, 2) = "TD on " & Date
End If
Next Cell5
Application.Calculation = xlCalculationAutomatic 'doesnt do anything
End Sub
它像发条一样工作,但是由于电子表格的性质,如果更改了多个单元格,则需要永远重新计算(因为它会在单元格更改后重新计算,然后运行宏,这会更改另一个单元格,因此它会再次重新计算并为每个更改的单元格重复该过程)当您更改超过 50 个单元格时,1.5 秒的计算时间变得难以忍受。
插入 Application.Calculation=xlCalculationManual
没有帮助,因为我需要它在宏完成后自动插入 运行,所以我必须把 Application.Calculation=xlCalculationAutomatic
放在最后。
我在这里不知所措,非常感谢任何帮助。
在处理 Worksheet_Change
事件以及 Calculation
模式更改时,始终使用:
Application.EnableEvents = False
当所有单元格都改变后,使用相反的方法再次启用它:
Application.EnableEvents = True