Wednesday 5 August 2015

Excel: Using cell comments to provide "drill-down" details

A previous article (link) adapted a sample panel from the BBC website to look at options for creating arrows in Excel to indicate direction of change:




The article covered different ways of creating arrows in Excel and the need to define tolerances to create thresholds for changing arrows. The example Excel rendering introduced additional columns to hold both the previous values and the tolerances. These would be hidden in a real, deployed dashboard.

Some Design Considerations

In terms of dashboard design, the BBC-style panel could be simplified. There is a hierarchy within the information. From most the important downwards, something like:
  • Markets  i.e. what is the whole panel about
  • Each Index and whether up or down (the most important information)
  • The latest value of each index (it could be argued that some people would find % change more important than the actual value) 
  • The % change and/or previous value of each index (in conjunction with present value, these are two versions of the same thing, although % change conveys more information quickly)
  • Full details of the activity of each index over a period of time  (a 'drill down' to the full details for each index can be provided in Excel - as well as on a website - as a hyperlink to a second page of details). 
Re-arranging the panel to reflect the hierarchy:




This simplified design makes the most important information readable at a glance and also relegates the calculated % change data from the panel. 

Cell Comments as a method for providing drill-down

One way to retain access to details from within the panel (without the need to jump to a different page via a hyperlink) is to use cell comments:

















Comments can be added to cells manually but this would be extremely time consuming to set up and maintain, and prone to error. To be of real use, the comments need to be generated automatically.

The Worksheet SelectionChange event provides a method for triggering updates. The example above uses the  .AddComment method in VBA to create the comments box itself. The text within the comments box needs to be formatted to control the number of decimal places. Separate lines within the box are created using 'Chr(10)' in the text string.

Cells with a comment are designated in Excel by a small red triangle in the top right hand corner. Microsoft appears to determined to make this feature unalterable. It cannot be adjusted through options, property settings or by using VBA. Workarounds are discussed variously on Excel forums: all seem to rely on creating a differently coloured triangle over the top of the red one. The approach taken here, to avoid having a dashboard covered in unsightly little red triangles, is to wait until the comments are needed before creating them and then delete them again afterwards, straight away.


The VBA code used to generate the example above is as follows:





The details are collated from hidden columns:
  • Column G: contains the percentage change
  • Column H: contains the previous value
  • Column K: contains a brief description of what the index is (for illustrative purposes)
The .visible property is set to true in order to open up the comments box.

Clicking on another cell within the target range in column E will trigger a three stage update:
  • The last generated  comment will be deleted
  • A new comment box will be constructed with the requisite information collated from the values for the same row in Columns G,H and K. 
  • The new box will be opened up (made Visible)
If a cell outside of the target range in column E is clicked on, the last generated  comment will be deleted but no new comment box will be created. All comments will have been removed and there will be no red triangles left behind.

The approach has some limitations: the size and style of the comments box is hard to control (it is not possible to set 'autosize' to true on creation); selecting more than one cell can cause strange results.


Other options

An alternative to using cell comments is to create a text box. This works in a similar way to cell comments. It introduces additional technical challenges, most notably the need to determine the positioning of the box.



References

The VBA used here is a combination of ideas gleaned from various Excel user forums and some original experimentation. Because a lot of material recirculates in forums, it is difficult to trace original ideas reliably back to their creators to give due credit. The following are just a few of those referred to:



Shapes.AddTextbox Method (Excel). Excel 2013 Developer Reference. Microsoft. https://msdn.microsoft.com/en-us/library/office/ff838832.aspx

VBA Tips - Find Out If A Cell Is Within A RangeExcelExperts.com.21 May, 2009. http://excelexperts.com/VBA-Tips-Find-Out-If-A-Cell-Is-Within-A-Range

Using the Intersect Method in Excel VBA

The Complete Guide to Ranges and Cells in Excel VBA by Paul Kelly.2nd January 2015. Excel Macro Mastery. 

How to change comment indicator color in Excel?. ExtendOffice.