在点击图表 - Excel VBA上获取X轴值 [英] Get X Axis value on the Click of Chart - Excel VBA
问题描述
我遇到了一个奇怪的要求。
当用户点击图表区域时,我需要从图表中获取X轴值。
我知道我们可以为图表分配一个宏。所以在方法上,可以创建图表的事件。
谢谢。
p>
如果您的图表在图表表中,您可以右键单击图表表选项卡,选择查看代码
选项显式
Private Sub Chart_Select(ByVal ElementID As long,ByVal Arg1 As Long,ByVal Arg2 As Long)
选择案例ElementID
案例xlSeries
如果Arg2> 0 Then
MsgBoxSeries& Arg1& vbCr& 点& Arg2
End If
Case Else
MsgBox Arg1& vbCr& Arg2
End Select
End Sub
如果您的图表已嵌入工作表,那么您必须使用 WithEvents
作为@brettdj已经涵盖了此处< a>
FOLLOWUP
这是您想要的吗? >
Private Sub Chart_Select(ByVal ElementID As Long,ByVal Arg1 As Long,ByVal Arg2 As Long)
选择案例ElementID
case xlSeries
如果Arg2> 0 then
x = ActiveChart.SeriesCollection(Arg1).XValues
For i = LBound(x)To UBound(x)
如果i = Arg2 then
MsgBoxPoint: ; i& 和X Value =& x(i)
退出对于
结束如果
下一个i
结束如果
结束选择
结束子
/ p>
I have come across a strange requirement.
I need to get X-Axis value from chart when user clicks on Chart Area.
I know we can assign a macro to a chart. So in way, event for the chart can be created. But no idea of how to proceed further.
Any idea to do this please?
Thanks.
If your Chart is in a Chart Sheet then you can right click on the Chart sheet tab, select "View code" and paste the following in its code module (see screenshot below)
Option Explicit
Private Sub Chart_Select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long)
Select Case ElementID
Case xlSeries
If Arg2 > 0 Then
MsgBox "Series " & Arg1 & vbCr & "Point " & Arg2
End If
Case Else
MsgBox Arg1 & vbCr & Arg2
End Select
End Sub
If your chart is embedded in a sheet then you will have to use WithEvents
as @brettdj has already covered here
FOLLOWUP
Is this what you are trying?
Private Sub Chart_Select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long)
Select Case ElementID
Case xlSeries
If Arg2 > 0 Then
x = ActiveChart.SeriesCollection(Arg1).XValues
For i = LBound(x) To UBound(x)
If i = Arg2 Then
MsgBox "Point :" & i & "and X Value = " & x(i)
Exit For
End If
Next i
End If
End Select
End Sub
这篇关于在点击图表 - Excel VBA上获取X轴值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!