2013-03-20 36 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