1

I have a chart with 14 series. Likewise I have 14 charts that are associated with each of these series. I am trying to create a macro that can be called when a particular series is selected. When the series is selected the macro makes the corresponding chart appear which gives the user more detailed data.

This is what I have come up with so far (Bear with me I am a weak programmer). Please let me know if I am on he right track and if not please give me some direction. this code currently gives me an error message "Object doesn't support this property or method".

Thanks!

Sub Macro1()

Dim Series6 As Object
Set Series6 = ActiveChart.SeriesCollection(1).Points(6)

   If Series6.Select Then
   Sheets("Sheet1").ChartObjects("Chart 2").Visible = True
   End If
End Sub
4

1 回答 1

3

您可以创建图表事件来处理此类事情

假设:

  • 图表已开启Sheet1
  • 主图被命名Chart 1
  • 详细图表以序列号 +1 命名(例如Series 1,与 相关Chart 2

要设置图表事件,请按照以下步骤操作

  1. 创建一个名为的类模块EventClassModule
  2. 向该模块添加代码

    Option Explicit
    
    Public WithEvents myChartClass As Chart
    
    Private Sub myChartClass_Select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long)
        Dim ChartName As String
        Dim i As Long
        If ElementID = 3 And Arg1 = 1 Then ' 3 indicates a Series
            ChartName = "Chart " & Arg2 + 1 ' Arg1 is the series number
            With Worksheets("Sheet1")
                ' Hide all sub charts
                For i = 2 To .ChartObjects.Count
                    .ChartObjects(i).Visible = False
                Next
                ' Show the required chart
                .ChartObjects(ChartName).Visible = True
            End With
        End If
    End Sub
    
  3. 初始化类模块(最好作为工作簿打开事件完成:将此代码放入ThisWorkbook模块中)

    Option Explicit
    
    Dim myClassModule As New EventClassModule
    
    Private Sub Workbook_Open()
        Set myClassModule.myChartClass = _
            Sheet1.ChartObjects("Chart 1").Chart
    End Sub
    

Now, when a series on Chart 1is selected, the related detail chart is shown, and the others are hidden

有用的链接
MSDN Chart Object Events
MSDN Chart Select Event

于 2013-10-18T20:25:04.527 回答