我不知道加载项,但可以通过图表交互在 VBA 中完成很多工作。只需插入图表表并将以下代码输入到 VBA 中的该表中。
这是我在我的工作图中的一个例子。当我单击一个系列时,它将创建一个文本框并在单元格中填充文本,该单元格在下面的代码中更新。它仅用于系列名称,但您可以为其添加更多功能。
Private Sub Chart_MouseDown(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
Dim ElementID As Long, Arg1 As Long, Arg2 As Long
Dim chart_data As Variant, chart_label As Variant
Dim last_bar As Long, chrt As Chart
Dim ser As Series, Txt As String
On Error Resume Next 'Sorry for this line of code, I haven't had the chance to look into why it was needed.
Me.GetChartElement x, y, ElementID, Arg1, Arg2
Set chrt = ActiveChart
Set ser = ActiveChart.SeriesCollection(1)
chart_data = ser.Values
chart_label = ser.XValues
Set txtbox = ActiveSheet.Shapes("hover") 'I suspect in the error statement is needed for this.
If ElementID = xlSeries Then
txtbox.Delete
Sheet1.Range("Ch_Series").Value = Arg1
Txt = Sheet1.Range("CH_Text").Value
Set txtbox = ActiveSheet.Shapes.AddTextbox _
(msoTextOrientationHorizontal, x - 150, y - 150, 150, 40)
txtbox.Name = "hover"
txtbox.Fill.Solid
txtbox.Fill.ForeColor.SchemeColor = 9
txtbox.Line.DashStyle = msoLineSolid
chrt.Shapes("hover").TextFrame.Characters.Text = Txt
With chrt.Shapes("hover").TextFrame.Characters.Font
.Name = "Arial"
.Size = 12
.ColorIndex = 16
End With
ser.Points(Arg2).Interior.ColorIndex = 44
txtbox.Left = x - 150
txtbox.Top = y - 150
Else
txtbox.Delete
ser.Interior.ColorIndex = 16
End If
End Sub
但是您也可以对悬停功能执行以下操作。
Private Sub Chart_MouseMove(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
请记住,代码需要插入图表工作表而不是模块中。
至于适合图表的数据范围,您是否尝试过动态命名范围,然后将图表设置为引用命名范围?
您可以设置 MouseMove 功能以显示您想要的内容,然后在 MouseDown 上它可以导航到选定的系列数据范围。
希望这可以帮助。