单击绘图轴 – 输出轴值到单元格

我想知道如果有人知道在Excel中使用VBA或任何其他方式的Excel中的方式:

交互地点击在Excel中绘制的graphics的x轴,并且将x轴值输出到单元格。

有人有主意吗? 我很难过

不是一件特别容易的事情

如果你创build一个事件处理程序来处理图表/图表坐标轴,那么你就可以开始了。 把它放在工作簿代码模块中。 当文件打开时,将根据Workbook_Open事件设置Cht 。 然后, Private Sub Cht_Select...将在用户select图表时运行。 如果选中的部件是一个轴,它将显示一个消息框。 您将需要想出一种方法来确定相对于轴的光标位置,并进行一些math计算来计算轴的值。

 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

然后,您需要获取轴位置和长度,并执行一些简单的math运算来计算光标所在的轴值。

这里是一个稍微修改过的版本,你可以把它放在一个标准模块中,以返回数组中的XY坐标。 您可以自己决定如何使用图表轴对象,最小/最大,长度,左侧和顶部值,以便在select轴时计算(或近似)光标的轴值。

 ' 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