Hover with mouse cursor to change stock in a candlestick chart
This article demonstrates how to change chart series while hovering with mouse cursor over a series name. The image above shows a stock chart and two company names in the top right corner.
Hover with the mouse cursor over a company name to trigger a macro. The macro checks which company and changes the chart data source accordingly.
The chart is instantly updated with the values corresponding to the chosen company (series name). The technique shown in this article can be applied to any kind of chart.
Rudy asks in this post Use a mouse hovering technique to create an interactive chart:
Is it possible to create this interactive chart into interactive candlestick chart to compare two or more charts ?
What you will learn in this article
- Create a hyperlink using the HYPERLINK function.
- Utilize a User defined Function (UDF) in the hyperlink formula.
- Explain how to change chart series programmatically.
- How to change axis min and max values programmatically.
- Where to put UDF code in a module of your workbook.
- Save the workbook with the correct file extension.
Hover over a company name and the chart instantly changes data source.
Create a hyperlink
To insert a hyperlink you have two options, manually add a hyperlink or use the HYPERLINK function. In this example, I will use the HYPERLINK function to create a hyperlink.
Formula in cell J2:
Formula in cell J3:
I will explain these formulas below.
Explaining formula in cell J2
I recommend that you use the "Evaluate Formula" tool to understand formula calculations in greater detail. Select a cell containing the formula you want to examine.
Go to tab "Formulas" on the ribbon. Press with left mouse button on "Evaluate Formula" button and a dialog box appear, see image above. Press with left mouse button on the "Evaluate" button to iterate through each step in the formula calculation, press with left mouse button on "Close" button to dismiss the dialog box.
Step 1 - HYPERLINK function
The HYPERLINK function has two arguments: HYPERLINK(link_location, [friendly_name])
Based on the docs the link_location argument can be the path to a file, file on a server, file on the internet, a workbook, webpage or a bookmark in a word document. However, it is also possible to use a User Defined Function that is rund when you hover over the link. This is not mentioned in the documentation.
The [friendly_name] argument is optional, we are going to use nothing "". If we type something here it will be formatted as a hyperlink in the worksheet which is not what we want. Step 3 below will explain how to show a text string without being formatted as a hyperlink.
HYPERLINK(MouseHover("Google"),"")
Step 2 - Prevent errors
The IFERROR function returns a value you specify if the first argument returns an error. This will prevent an error appearing if something goes wrong with the HYPERLINK function or the UDF.
IFERROR(HYPERLINK(MouseHover("Google"),""),"")
Step 3 - Append text
The ampersand character allows you to concatenate two or more strings. This will make sure that a value is shown in the cell. The hover will work anyway with an empty cell, however, it will certainly confuse the user when the UDF is rund perhaps by accident.
"Google"&IFERROR(HYPERLINK(MouseHover("Google"),""),"")
User defined function
'Name user defined function, dimension argument variable and declare it's data type Function MouseHover(str As String) 'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. With ActiveSheet.ChartObjects("Chart 2") 'Check if variable str meets condition and value in cell A1 in sheet1 is not equal to variable str If str = "Google" And Sheet1.Range("A1") <> str Then 'Save text string "Google" to cell A1 Sheet1.Range("A1") = "Google" 'Change chart source to "A2:A54,C2:F54" in worksheet name specified in cell A1 .Chart.SetSourceData Source:=Sheets("" & Range("A1") & "").Range("A2:A54,C2:F54") 'Change axis max value based on number in cell K2 .Chart.Axes(xlValue).MaximumScale = Range("K2") 'Change axis min value based on number in cell L2 .Chart.Axes(xlValue).MinimumScale = Range("L2") 'Check if variable str equals "Apple" and cell A1 is not equal to variable str ElseIf str = "Apple" And Sheet1.Range("A1") <> str Then 'Change cell contents in cell A1 Sheet1.Range("A1") = "Apple" 'Change chart source to "A2:A54,C2:F54" in worksheet name specified in cell A1 .Chart.SetSourceData Source:=Sheets("" & Range("A1") & "").Range("A2:A54,C2:F54") 'Change axis max value based on number in cell K3 .Chart.Axes(xlValue).MaximumScale = Range("K3") 'Change axis min value based on number in cell L3 .Chart.Axes(xlValue).MinimumScale = Range("L3") End If End With End Function
Where to put the code?
- Press Alt + F11 to open the Visual Basic Editor.
- Select your workbook in the Project Explorer.
- Press with left mouse button on "Insert" on the menu.
- Press with left mouse button on "Module" to create a module in your workbook.
- Copy above VBA code.
- Paste to code module, see image above.
Calculate max and min values for chart axis
The formulas below returns the min and max values for each chart source using the min and max values in cell range D2:D54.
Formula in cell K2:
Formula in cell K3:
Formula in cell L2:
Formula in cell L3:
Built-in Charts
Combo Charts
Combined stacked area and a clustered column chartCombined chart – Column and Line on secondary axis
Combined Column and Line chart
Chart elements
Chart basics
How to create a dynamic chartRearrange data source in order to create a dynamic chart
Use slicers to quickly filter chart data
Four ways to resize a chart
How to align chart with cell grid
Group chart categories
Excel charts tips and tricks
Custom charts
How to build an arrow chartAdvanced Excel Chart Techniques
How to graph an equation
Build a comparison table/chart
Heat map yearly calendar
Advanced Gantt Chart Template
Sparklines
Win/Loss Column LineHighlight chart elements
Highlight a column in a stacked column chart no vbaHighlight a group of chart bars
Highlight a data series in a line chart
Highlight a data series in a chart
Highlight a bar in a chart
Interactive charts
How to filter chart dataHover with mouse cursor to change stock in a candlestick chart
How to build an interactive map in Excel
Highlight group of values in an x y scatter chart programmatically
Use drop down lists and named ranges to filter chart values
How to use mouse hover on a worksheet [VBA]
How to create an interactive Excel chart [VBA]
Change chart series by clicking on data [VBA]
Change chart data range using a Drop Down List [VBA]
How to create a dynamic chart
Animate
Line chart Excel Bar Chart Excel chartAdvanced charts
Custom data labels in a chartHow to improve your Excel Chart
Label line chart series
How to position month and year between chart tick marks
How to add horizontal line to chart
Add pictures to a chart axis
How to color chart bars based on their values
Excel chart problem: Hard to read series values
Build a stock chart with two series
Change chart axis range programmatically
Change column/bar color in charts
Hide specific columns programmatically
Dynamic stock chart
How to replace columns with pictures in a column chart
Color chart columns based on cell color
Heat map using pictures
Dynamic Gantt charts
Stock charts
Build a stock chart with two seriesDynamic stock chart
Change chart axis range programmatically
How to create a stock chart
Excel categories
3 Responses to “Hover with mouse cursor to change stock in a candlestick chart”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form
OMG, you're really genius. Thank you very much
Mr. Oscar, can you help me once again, how to apply horizontal line or diagonal line based on cell value in this interactive candlestick chart so I can make target or risk reward ratio. Can we make that line in excel ? Thank you Mr. Oscar for the reply. You are the best.
Rudy,
Thanks. This post explains how to add series to a stock chart:
https://peltiertech.com/Excel/Charts/StockChartPlus.html