簡體   English   中英

Excel 2003 - 如何在拖動圖表點時觸發事件?

[英]Excel 2003 - How do I trigger an event when a chart point is dragged?

我有一個圖表,它是三次樣條函數發生器輸出的 XY 圖。 樣條的輸入是一系列點(X, Y) ,這些點也顯示在圖形上。 樣條輸出的計算由 VBA Worksheet_Change事件觸發。 這個想法是曲線生成是交互式的——用戶輸入一個 XY 對,樣條輸出圖相應地改變。

問題是當我通過用鼠標單擊並拖動一個點來更改點坐標時,單元格中的相應值會發生變化,但未觸發事件。 如果我手動更改該值,則會按預期觸發該事件。

有沒有辦法在圖表上拖放一個點時生成事件?

** 更新 **

我添加了一些錯誤處理以確保在重新計算引發異常時再次設置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()事件和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

.

更新:

我更正了:您的錯誤處理很好。 我認為錯誤處理不適用於“子訂閱”,但快速測試證明我不正確:

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

在測試中,“堆棧”和“溢出”錯誤均未顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM