2013-03-20 37 views

回答

1

不是一個特別容易的事......

如果您創建當你seelct圖/圖表軸的事件處理程序,這將讓你開始。將其放入工作簿代碼模塊中。當文件打開時,Cht將根據Workbook_Open事件設置。然後Private Sub Cht_Select...將在用戶選擇圖表時運行。如果選定的零件是一個軸,它將顯示一個消息框。您需要想出一種方法來確定相對於軸的光標位置,並進行一些數學計算來計算軸值。

Option Explicit 

Public WithEvents Cht As Chart 
Private Sub Cht_Select(ByVal ElementID As Long, _ 
    ByVal Arg1 As Long, ByVal Arg2 As Long) 

    If ElementID = xlAxis Then 
    MsgBox "Call your macro here to identify cursor position in chart..." 
    End If 

End Sub 
Private Sub Workbook_BeforeClose(Cancel As Boolean) 
    Set Cht = Nothing 
End Sub 

Private Sub Workbook_Open() 
    Set Cht = Sheet1.ChartObjects(1).Chart 
End Sub 

有關於獲取鼠標光標的位置,這裏的一些信息:

http://support.microsoft.com/kb/152969

你會再需要獲得軸位置&長度,並做一些簡單的數學來計算軸-value指針所在的位置。

這是一個稍作修改的版本,您可以將它放在標準模塊中,以返回數組中的XY座標。由您決定如何使用這些圖表軸對象,最小/最大值,長度,左邊&頂部值,以便在選擇軸時計算(或近似)光標的軸值。

' Access the GetCursorPos function in user32.dll 
    Declare Function GetCursorPos Lib "user32" _ 
    (lpPoint As POINTAPI) As Long 
    ' Access the GetCursorPos function in user32.dll 
    Declare Function SetCursorPos Lib "user32" _ 
    (ByVal x As Long, ByVal y As Long) As Long 

    ' GetCursorPos requires a variable declared as a custom data type 
    ' that will hold two integers, one for x value and one for y value 
    Type POINTAPI 
    X_Pos As Long 
    Y_Pos As Long 
    End Type 

    ' This function retrieve cursor position: 
    Function Get_Cursor_Pos() As Variant 

    ' Dimension the variable that will hold the x and y cursor positions 
    Dim Hold As POINTAPI 
    Dim xyArray(1) As Variant 

    ' Place the cursor positions in variable Hold 
    GetCursorPos Hold 

    ' Display the cursor position coordinates 
    xyArray(0) = Hold.X_Pos 
    xyArray(1) = Hold.Y_Pos 
    Get_Cursor_Pos = xyArray 
    End Function 

Sub GetCoordinates() 
    Dim xyPosition As Variant 

    xyPosition = Get_Cursor_Pos 

    Debug.Print xyPosition(0) 
    Debug.Print xyPosition(1) 

    '### Now that you have the x and y position, you will need to perform some 
    ' additional computations with the Axis' .top, .left, and its min/max values 
    ' in order to get the approximate axis location where you mouse-clicked it. 


End Sub