Excel 2003 - 如何在拖动图表点时触发事件?

时间:2017-10-30 22:46:27

标签: excel vba events worksheet-function excel-2003

我有一个图表,它是三次样条函数发生器输出的XY图。样条曲线的输入是一系列点(X, Y),这些点也显示在图表上。样条输出的计算由VBA Worksheet_Change事件触发。我们的想法是曲线生成是交互式的 - 用户输入X-Y对,样条输出图也相应地改变。

问题是当我通过用鼠标单击并拖动一个点来改变点坐标时,单元格中的相应值会发生变化,但事件不会被触发。如果我手动更改值,则会按预期触发事件。

当在图表上拖放一个点时,有没有办法生成事件?

**更新**

我添加了一些错误处理,以确保在重新计算抛出异常时再次设置EnableEvents标志:

private Sub Worksheet_Calculate()

Application.EnableEvents     = False   ' make sure there are no recursive calls
On Error GoTo Finalize                 ' make sure events are re-enabled if we crash in here

RecalculateOutputPoints
Finalize:
    Application.EnableEvents = True
End Sub

1 个答案:

答案 0 :(得分:1)

使用Worksheet_Calculate()事件和EnableEvents属性:

Private Sub Worksheet_Calculate()

    Application.EnableEvents     = False   ' make sure there are no recursive calls
    On Error GoTo Finalize                 ' make sure events are re-enabled if we crash in here
    Call RecalculateOutputPoints()

  Finalize:
    Application.EnableEvents = True

End Sub

Sub RecalculateOutputPoints()

    On Error GoTo Finalize                 ' make sure events are re-enabled 
    ...your code here...

  Finalize:
    Application.EnableEvents=True

End Sub

更新

我的观点是纠正的:您的错误处理很好。我认为错误处理并不适用于" child subs"但快速测试证明我不正确:

Sub RunThisSub()
    On Error GoTo gotError
    Call causeError
    Err.Raise 28 'cause "Stack" error
gotError:
    MsgBox "This is after the error"
End Sub

Sub causeError()
    Err.Raise 6 'cause "Overflow" error
End Sub

在测试中," Stack"也不是"溢出"显示错误。