Home » excel » excel – Looking to add High and Low toolstips to xlStockOHLC candlestick charts in VBA

excel – Looking to add High and Low toolstips to xlStockOHLC candlestick charts in VBA

Posted by: admin May 14, 2020 Leave a comment


Running on Windows.
I see plenty of examples on SO but they’re all in JS.
I am using VBA and creating my candlestick chart with the following:

    OHLCChartObject.name = OHLCChartName
        With OHLCChartObject.Chart
        .SetSourceData Source:=getOHLCChartSource
        .ChartType = xlStockOHLC
        .Axes(xlCategory).CategoryType = xlCategoryScale
        .HasTitle = True
        .ChartTitle.Text = ""
        .HasLegend = False
        With .ChartGroups(1)
            .UpBars.Interior.ColorIndex = 10
            .DownBars.Interior.ColorIndex = 3
        End With
    End With
End Sub

Is there any way to add tooltips to show the actual Open/High/Low/Close values?

How to&Answers:

May try Some workaround this.

enter image description here

Instead of changing Toolstip, in test it is used to show values in a Shape “Rectangle 2” embedded in the chart itself. However it could be easily modified to show the results along with the title or Datalabel of the point with mouse move.

Create a class module named XChart with Chart Events

Class module Code

Edited: added additional functionality of modifying data labels.

Option Explicit
Public WithEvents Ohlc As Chart
Public Arr1 As Variant, Arr2 As Variant, Arr3 As Variant, Arr4 As Variant
Private Sub Ohlc_MouseMove(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
Dim IDNum As Long, a As Long, b As Long
Dim i As Long, txt As String, ht As Long, txt2 As String
Ohlc.GetChartElement x, y, IDNum, a, b

If IDNum <> xlSeries Then
'Finding XlSeries in OHLC chart is little difficult
'So try all Y values correspoding to X in the chart to find XlSeries
'However this compromise performace
ht = Ohlc.Parent.Height
    For y = 1 To ht
    Ohlc.GetChartElement x, y, IDNum, a, b   'c, d
    If IDNum = xlSeries Then Exit For
End If

If IDNum = xlSeries Then
' For Test purpose, May delete next 5 lines
ActiveSheet.Range("L1").Value = x
ActiveSheet.Range("L2").Value = y
ActiveSheet.Range("L3").Value = IDNum
ActiveSheet.Range("L4").Value = a
ActiveSheet.Range("L5").Value = b
    If b > 0 Then
    ActiveSheet.Range("M1").Value = Arr1(b)  ' For Test purpose, may delete
    txt = "Open: " & Arr1(b) & " High: " & Arr2(b) & vbCrLf & _
            "Low: " & Arr3(b) & " Close: " & Arr4(b)
    txt2 = "O: " & Arr1(b) & " H: " & Arr2(b) & _
            " L: " & Arr3(b) & " C: " & Arr4(b)
    Ohlc.Shapes("Rectangle 2").TextEffect.Text = txt

       For i = 1 To Ohlc.SeriesCollection(1).Points.Count
       With Ohlc.SeriesCollection(1).Points(i)
            If i = b Then
            .HasDataLabel = True
            .DataLabel.Text = txt2
            .HasDataLabel = False
            End If
       End With
    End If
End If
End Sub
Public Sub Storevalues()
Arr1 = Ohlc.SeriesCollection(1).Values
Arr2 = Ohlc.SeriesCollection(2).Values
Arr3 = Ohlc.SeriesCollection(3).Values
Arr4 = Ohlc.SeriesCollection(4).Values
End Sub

Next in the VBA Code in standard module where Chart was created or in some other event / procedure, set the Chart as new XChart. For test an already existing chart is used. it may also be used at workbook open event.

Public XOhlc As New XChart
Sub initChart()
Dim Ch As Chart
'Modify the line to your requirement
Set Ch = ThisWorkbook.Worksheets("Sheet1").ChartObjects("Chart 3").Chart
Set XOhlc.Ohlc = Ch
End Sub

All the Sheet,Chart, Shape etc names may please be modified to requirement.


Do you want Tooltips, or Data Labels?

A candlestick chart has Tooltips that appear when your mouse moves over any of the data points (high or low at the ends of the whiskers, open or close at the ends of the boxes).

Data Labels are permanent labels adjacent to a chart’s data points. These are added by clicking the plus icon floating beside the chart and checking the box next to Data Labels, or finding the relevant command on the ribbon. I fear that data labels on every point will make the chart cluttered.