2016-08-16 2 views
0

선택한 포인트에 대한 시리즈에서 XValue를 찾는 더 좋은 (더 예쁘다) 방법을 찾고 있습니다. 포인트는 사용자에 의해 (클릭함으로써) 선택되고 따라서 인덱스는 알려지지 않습니다. 현재의 메소드는 각 포인트를 반복하고 이름이 이론 상으로는 서로 다른 두 점에서 같을 수 있기 때문에 그다지 우아하지 않은 이름을 비교합니다.포인트 오브젝트에서 XValue 또는 Value를 찾으십시오.

XValues는 날짜이며 두 가지 유형의 차트가 있습니다. 시리즈 이름은 데이터 테이블에있는 날짜이거나 포인트 XValue는 데이터 테이블에있는 날짜입니다. 아래 코드는 클래스 모듈의 코드입니다. 차트의 데이터가 선택되면 데이터 테이블의 해당 행도 선택해야합니다.

Option Explicit 
' Declare object of type "Chart" with events 
Public WithEvents EvtChart As Chart 
Private Sub EvtChart_Select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long) 
    Dim d As Date, r As Range 
    If TypeOf Selection Is Series And IsDate(Selection.Name) Then 
     d = CDate(Selection.Name) 
    ElseIf TypeOf Selection Is Point Then 
     If IsDate(Selection.Parent.Name) Then 
      d = CDate(Selection.Parent.Name) 
     Else 
      Dim s As Series, p As Point, i As Long 
      Set s = Selection.Parent 
      Set p = Selection 
      For i = 1 To s.Points.Count 
       If p.Name = s.Points(i).Name Then 
        d = s.XValues(i) 
        Exit For 
       End If 
      Next 
     End If 
    Else 
     Exit Sub 
    End If 
    Set r = Range(Summary.Cells(HROW + 2, 1), Summary.Cells(1048576, 1).End(xlUp)).Find(d, , xlFormulas) 
    If Not r Is Nothing Then r.EntireRow.Select 
End Sub 
+0

설정 한 사진이 포함 된 사진을 요청에 포함시킬 수 있습니까? –

+0

질문을 이해할 수 없습니다. 이 차트에이 이벤트를 추가하는 제외한 전체 코드는 다음과 같습니다. 새로운 CEventChart 하위 InitEvents 희미한 clsEventChart() 설정 clsEventChart.EvtChart = Summary.ChartObjects ("DynChart")에 시도 – j74nilsson

+0

최종 하위 차트 귀하의 질문을 게시하십시오 [이 답변을 좋아합니다] (http://stackoverflow.com/a/38949184/3397819) –

답변

0

@alexricher가 그의 의견에서 지적했듯이, Arg2는 필요한 색인을 제공합니다. 수정 된 코드는 다음과 같습니다

Option Explicit 
' Declare object of type "Chart" with events 
Public WithEvents EvtChart As Chart 
Private Sub EvtChart_Select(ByVal ElementID As Long, ByVal Arg1 As Long, ByVal Arg2 As Long) 
    Dim d As Date, r As Range 
    If TypeOf Selection Is Series Then 
     If IsDate(Selection.Name) Then d = CDate(Selection.Name) 
    ElseIf TypeOf Selection Is Point Then 
     If IsDate(Selection.Parent.Name) Then 
      d = CDate(Selection.Parent.Name) 
     Else 
      Dim s As Series 
      Set s = Selection.Parent 
      d = s.XValues(Arg2) 
     End If 
    Else 
     Exit Sub 
    End If 
    Set r = Range(Summary.Cells(HROW + 2, 1), Summary.Cells(1048576, 1).End(xlUp)).Find(d, , xlFormulas) 
    If Not r Is Nothing Then r.EntireRow.Select 
End Sub 

내가 또 다른 차트 요소 (예 : 축 등)을 선택하면 런타임 오류를 피할 수 If TypeOf Selection Is Series Then 라인에 작은 수정을했다.

관련 문제