简体   繁体   English

Excel中Worksheet_Calculate事件的行为–存储事件?

[英]Behaviour of Worksheet_Calculate event in Excel – stored events?

and thank you for reading my post. 并感谢您阅读我的文章。 I have encountered a strange (in my opinion) behaviour of Worksheet_Calculate event in Excel (Windows 7 Ultimate, Excel 2010), let me describe the problem I'm having by giving you a set up. 我在Excel(Windows 7 Ultimate,Excel 2010)中遇到了Worksheet_Calculate事件的奇怪(我认为)行为,请允许我通过设置来描述我遇到的问题。

Take a Workbook with 2 Sheets. 拿一张2张工作簿。 Sheet 2 Cell A1 has a formula =Sheet1!A1, EnableCalculation property of Sheet2 is set to TRUE and in Sheet2 Object there is a Sub 工作表2的单元格A1具有公式= Sheet1!A1,工作表2的EnableCalculation属性设置为TRUE,并且在工作表2对象中有一个子

Private Sub Worksheet_Calculate()
    i = MsgBox("Value " & Me.Range("A1").Value, vbOKOnly)
End Sub

In Module1 there is a SUB 在Module1中有一个SUB

Public Sub mySub()
    Application.EnableEvents = False
    ThisWorkbook.Worksheets(2).EnableCalculation = True
    ThisWorkbook.Worksheets(2).EnableCalculation = False
    Application.EnableEvents = True
End Sub

Now, enter 1 in Sheet1 Cell A1 – a message box with “Value 1” is displayed. 现在,在Sheet1单元格A1中输入1 –显示带有“值1”的消息框。 Next, set EnableCalculation property of Sheet2 to FALSE and enter 2 in Sheet1 Cell A1 – nothing happens. 接下来,将Sheet2的EnableCalculation属性设置为FALSE,然后在Sheet1单元格A1中输入2-什么也不会发生。 Now, run the mySub in Module 1 – Cell A1 on Sheet2 displays 2 and EnableCalculation property of Sheet2 is set to FALSE. 现在,在模块1中运行mySub – Sheet2上的单元格A1显示2,并且Sheet2的EnableCalculation属性设置为FALSE。 So far, so good. 到现在为止还挺好。 Now enter 3 in Sheet1 Cell A1 – a message box with “Value 2” is displayed! 现在在Sheet1单元格A1中输入3 –将显示一个带有“值2”的消息框!

It would seem that during the execution of mySub a Calculation event was fired (even though Application.EnableEvents was set to FALSE) and the value of 2 was stored (where?) and then this event was released when 3 was entered into Cell A1 on Sheet1 (even though EnableCalculation is set to FALSE for Sheet2). 似乎在执行mySub的过程中触发了一个计算事件(即使Application.EnableEvents设置为FALSE),并且存储了值2(在哪里?),然后在将3输入到单元格A1中时释放了该事件。 Sheet1(即使将Sheet2的EnableCalculation设置为FALSE)。 Any clue what's going on here and how to “fix” this strange behaviour? 任何线索,这是怎么回事,以及如何“修复”这种奇怪的行为?

Many thanks. 非常感谢。

Quote from http://www.decisionmodels.com 引用http://www.decisionmodels.com

'Setting enablecalculation to false and then back to true will flag all the formulae on the worksheet as uncalculated. '将enablecalculation设置为false,然后恢复为true,会将工作表上的所有公式标记为未计算。 If the calculation mode is Automatic a recalculation will be triggered. 如果计算模式为自动,将触发重新计算。 All calculation methods, including Sheet.Calculate but excluding Range.Calculate, will then calculate all the formulae on the sheet. 然后,所有计算方法(包括Sheet.Calculate,但不包括Range.Calculate)将计算工作表上的所有公式。 You can use this method as a way of simulating Calculatefull at worksheet rather than workbook level. 您可以使用此方法作为在工作表而非工作簿级别模拟Calculatefull的方法。 Note that Sheet.Calculate will not reset the uncalculated formulae flags, so two Sheet.Calculates in succession after toggling the EnableCalculation property will both do a Full Sheet Calculate.' 请注意,Sheet.Calculate不会重置未计算的公式标志,因此,在切换EnableCalculation属性后,两个连续的Sheet.Calculates都将执行“完整工作表计算”。

This seems to fix it 这似乎可以解决它

Public Sub mySub()
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    ThisWorkbook.Worksheets(2).EnableCalculation = True
    ThisWorkbook.Worksheets(2).EnableCalculation = False
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM