我希望Excel在每次用户输入值(source1 source2)时计算工作表:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Worksheet("Calc").Range("R1:R100")
MsgBox "Check!"
Application.EnableEvents = False
If Not Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
' Display a message when one of the designated cells has been changed.
MsgBox "Cell " & Target.Address & " has changed."
'Calculate
Sheets("Calc").Calculate
End If
Application.EnableEvents = True
End Sub此工作表在输入值后不进行重新计算。甚至,消息框“选中!”从来没有出现过。-为什么?
(关于信息:我只能猜测这与我将计算设置为“手册”有关):
我在工作簿"B.xlsm“中有一个带有循环引用的工作表,因此我从工作簿"A.xlsm”运行一个脚本,在那里打开工作簿"B.xlsm":
Application.Calculation = xlManual
[...]
Set wb = Workbooks.Open(strPath)是否有办法使此方法“更改”用于手工计算?)
发布于 2018-08-01 23:52:33
我想蒂姆搞定了。
在测试代码的某一时刻,代码停止;要么来自和运行时错误,要么手动逐行穿行。在这两种情况下,您可能更改了一些强制它重置的内容,并且它从来没有机会运行最重要的行.Application.EnableEvents = True。
禁用事件是一种持久状态。
如果其他用户要使用您的代码,那么确保您有错误处理来恢复它是非常明智的。不幸的是,您仍然会在测试时破坏它。
当发生这种情况时,我使用以下例程(在主模块的顶部)来修复它:
Public Sub FixIt()
Application.EnableEvents = True
If Not ActiveWindow Is Nothing Then Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub事实上,我通常把这个例程称为我在丝带/工作表上的任何按钮的第一行,只是为了确保一切正常工作。
在这种情况下,可以省略Application.Calculation行,或者将其设置为xlManual
https://stackoverflow.com/questions/51643245
复制相似问题