Click here to Skip to main content
15,881,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
getchart element function in excel vba code not return data until the sheet containing the embedded chart activated once

What I have tried:

Private Sub Image3_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    On Error Resume Next
    Const CNST_DISTFROMDATAPOINT = 20
    Dim IDNum As Long
    Dim a As Long
    Dim b As Long
    Dim Chrt As Chart
    
    'WZoom = ActiveWindow.Zoom / 100
    WZoom = 1
    C = PointsPerPixel / WZoom
    CW = DteIntrvlSheet.ChartObjects("Chart Intrvl").Width
    CH = DteIntrvlSheet.ChartObjects("Chart Intrvl").Height
    Set Chrt = DteIntrvlSheet.ChartObjects("Chart Intrvl").Chart
    IW = Image3.Width
    IH = Image3.Height
    CX = Round(X / IW * CW / C, 0)
    CY = Round(Y / IH * CH / C, 0)
    Chrt.GetChartElement CX, CY, IDNum, a, b
    If IDNum = xlSeries Then
        ID = (a + 1) \ 2
        Frame2.Caption = DteIntrvlSheet.Range("A2").Offset(b, 0).Value
        Label31.Caption = DteIntrvlSheet.Range("A1").Offset(0, ID).Value
        Label32.Caption = "Volume: " & Format(DteIntrvlSheet.Range("A2").Offset(b, ID).Value, "#,##") & " (Lit.)"
        Select Case a Mod 2
            Case 0
                Label33.Caption = DteIntrvlSheet.Range("A2").Offset(b, ID + 8).Value
                Frame2.Height = 80
            Case 1
                Label33.Caption = ""
                Frame2.Height = 34
        End Select
        With Frame2
            FL = X + CNST_DISTFROMDATAPOINT
            FT = Y + CNST_DISTFROMDATAPOINT
            FW = .Width
            FH = .Height
            ' Adjust the position, if tooltip at default position would not fit onto the Image area
            If FL + FW > Image3.Left + Image3.Width Then
                FL = FL + (-FW - 2 * CNST_DISTFROMDATAPOINT)
            End If
            If FT + FH > Image3.Top + Image3.Height Then
                FT = FT + (-FH - 2 * CNST_DISTFROMDATAPOINT)
            End If
            .Left = FL
            .Top = FT
            ' Make the tooltip visible
            .Visible = True
        End With
    Else
        Frame2.Visible = False
    End If
End Sub
Posted
Updated 3-Sep-19 0:49am

1 solution

I guess you have to activate the sheet programmatically then:
Worksheet.Activate method (Excel)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900