gpt4 book ai didi

vba - 单击图表时获取 X 轴值 - Excel VBA

转载 作者:行者123 更新时间:2023-12-03 01:06:56 25 4
gpt4 key购买 nike

我遇到了一个奇怪的需求。

I need to get X-Axis value from chart when user clicks on Chart Area.

我知道我们可以为图表分配一个宏。这样,就可以创建图表的事件。但不知道如何进一步进行。

请问有什么想法吗?

谢谢。

最佳答案

如果您的图表位于图表工作表中,则可以右键单击图表工作表选项卡,选择“查看代码”并将以下内容粘贴到其代码模块中(请参见下面的屏幕截图)

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

enter image description here

如果您的图表嵌入在工作表中,那么您将必须使用 WithEvents,因为 @brettdj 已经介绍过 here

跟进

这就是你正在尝试的吗?

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

enter image description here

关于vba - 单击图表时获取 X 轴值 - Excel VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15703967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com