Excel 2003 - 如何在拖动图表点时触发事件?
Excel 2003 - How do I trigger an event when a chart point is dragged?
我有一张图表,它是三次样条函数发生器输出的 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
使用 Worksheet_Calculate()
event and the 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"和"Overflow"错误均未显示。
我有一张图表,它是三次样条函数发生器输出的 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
使用 Worksheet_Calculate()
event and the 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"和"Overflow"错误均未显示。