{"id":670,"date":"2010-06-17T12:34:30","date_gmt":"2010-06-17T16:34:30","guid":{"rendered":"http:\/\/blog.tomleslie.ca\/?p=670"},"modified":"2010-06-18T12:06:50","modified_gmt":"2010-06-18T16:06:50","slug":"670","status":"publish","type":"post","link":"http:\/\/blog.tomleslie.ca\/?p=670","title":{"rendered":"Sparklines"},"content":{"rendered":"<p>A <a href=\"http:\/\/en.wikipedia.org\/wiki\/Sparkline\">sparkline<\/a> is a miniature line graph that shows trending information over a time period. It is designed to be shown next to the name of a data type in a table of data to show a quick &#8220;at a glance&#8221; reference of how the data is trending over time. Microsoft is introducing sparklines as a patented new feature in Excel 2010, which is causing some controversy as the concept was actually invented by Edward Tufte. In any case, I wanted to use sparklines now, not wait for Office 2010, so I wrote the macro below.<\/p>\n<p>To use it, select your data set (must be in a single row but need not be contiguous) and then (holding down the control button) the cell where you want the sparkline inserted. Then run the macro, which should create a nice little line graph and align it to the cell you&#8217;ve chosen.<\/p>\n<p>Please note that the macro has no error detection &amp; handling logic of any kind so if your experience is not a happy one you are completely on your own. Have a nice day.<br \/>\n<!--more--><br \/>\n<code><br \/>\nSub Sparkline()<br \/>\n'<br \/>\n' Sparkline Macro<br \/>\n' Macro written 14\/06\/2010 by Tom Leslie<br \/>\n'<br \/>\n' Creates a sparkline graph and inserts it into the selected cell (the last in the range)<\/code><\/p>\n<p>Dim iRangeWidth As Integer<br \/>\nDim rTargetCell As Range<br \/>\nDim rDataRange As Range<br \/>\nDim iMin, iMax As Double<br \/>\nDim sSheet As Worksheet<br \/>\nDim cChart As Chart<br \/>\nDim strChartName As String<\/p>\n<p>Set rTargetCell = Selection.Areas(Selection.Areas.Count).Cells(Selection.Areas(Selection.Areas.Count).Cells.Count)<\/p>\n<p>If Selection.Areas.Count = 1 Then<br \/>\niRangeWidth = Selection.Cells.Count &#8211; 1<br \/>\nSet rDataRange = Range(Selection.Cells(1), Selection.Cells(iRangeWidth))<br \/>\nElse<br \/>\nSet rDataRange = Selection.Areas(1)<br \/>\niRangeWidth = rDataRange.Cells.Count<br \/>\nFor i = 2 To Selection.Areas.Count &#8211; 1<br \/>\nSet rDataRange = Union(rDataRange, Selection.Areas(i))<br \/>\niRangeWidth = rDataRange.Cells.Count<br \/>\nNext i<br \/>\nEnd If<\/p>\n<p>iMin = Application.Min(rDataRange)<br \/>\niMax = Application.Max(rDataRange)<\/p>\n<p>rDataRange.Select<br \/>\nSet sSheet = ActiveSheet<\/p>\n<p>Charts.Add<br \/>\nSet cChart = ActiveChart<br \/>\nActiveChart.ChartType = xlLine<br \/>\nActiveChart.SetSourceData Source:=rDataRange, PlotBy:=xlRows<br \/>\nActiveChart.Location Where:=xlLocationAsObject, Name:=sSheet.Name<br \/>\nWith ActiveChart.Axes(xlValue)<br \/>\n.MinimumScale = iMin &#8211; (iMax &#8211; iMin) * (1 \/ rTargetCell.Height)<br \/>\n.MaximumScale = iMax + (iMax &#8211; iMin) * (1 \/ rTargetCell.Height)<br \/>\n.ReversePlotOrder = False<br \/>\n.ScaleType = xlLinear<br \/>\n.DisplayUnit = xlNone<br \/>\nEnd With<br \/>\nWith ActiveChart<br \/>\n.HasAxis(xlCategory, xlPrimary) = False<br \/>\n.HasAxis(xlValue, xlPrimary) = False<br \/>\nEnd With<br \/>\nActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic<br \/>\nWith ActiveChart.Axes(xlCategory)<br \/>\n.HasMajorGridlines = False<br \/>\n.HasMinorGridlines = False<br \/>\nEnd With<br \/>\nWith ActiveChart.Axes(xlValue)<br \/>\n.HasMajorGridlines = False<br \/>\n.HasMinorGridlines = False<br \/>\nEnd With<br \/>\nActiveChart.HasLegend = False<br \/>\nWith Selection.Border<br \/>\n.Weight = 1<br \/>\n.LineStyle = 0<br \/>\nEnd With<br \/>\nSelection.Interior.ColorIndex = xlNone<br \/>\nActiveChart.PlotArea.Select<br \/>\nWith Selection.Border<br \/>\n.Weight = xlThin<br \/>\n.LineStyle = xlNone<br \/>\nEnd With<br \/>\nSelection.Interior.ColorIndex = xlNone<br \/>\nActiveChart.SeriesCollection(1).Select<br \/>\nWith Selection.Border<br \/>\n.ColorIndex = 17<br \/>\n.Weight = xlMedium<br \/>\n.LineStyle = xlContinuous<br \/>\nEnd With<br \/>\nWith Selection<br \/>\n.MarkerBackgroundColorIndex = xlNone<br \/>\n.MarkerForegroundColorIndex = xlNone<br \/>\n.MarkerStyle = xlNone<br \/>\n.Smooth = False<br \/>\n.MarkerSize = 3<br \/>\n.Shadow = False<br \/>\nEnd With<br \/>\nActiveChart.PlotArea.Height = rTargetCell.Height \/ 2<br \/>\nActiveChart.Refresh<br \/>\nDebug.Print ActiveChart.PlotArea.Height<br \/>\nstrChartName = Right(ActiveChart.Name, Len(ActiveChart.Name) &#8211; Len(sSheet.Name))<br \/>\nWith ActiveSheet.Shapes(strChartName)<br \/>\n.Left = rTargetCell.Left<br \/>\n.Top = rTargetCell.Top &#8211; 3<br \/>\n.LockAspectRatio = False<br \/>\n.Width = rTargetCell.Width<br \/>\n.Height = rTargetCell.Height + 6<br \/>\nDebug.Print &#8220;Target:&#8221; &amp; .Height<br \/>\nDebug.Print ActiveChart.ChartArea.Height<br \/>\nEnd With<br \/>\nActiveChart.Refresh<br \/>\nWith ActiveChart.PlotArea<br \/>\n.Left = 0<br \/>\n.Top = 0<br \/>\nEnd With<br \/>\nActiveSheet.Cells(1, 1).Select<br \/>\nsSheet.ChartObjects(strChartName).Select<br \/>\nActiveChart.PlotArea.Height = rTargetCell.Height<br \/>\nActiveChart.PlotArea.Width = rTargetCell.Width<br \/>\nDebug.Print &#8220;Height:&#8221; &amp; ActiveChart.ChartArea.Height &amp; &#8220;:&#8221; &amp; ActiveChart.PlotArea.Height<\/p>\n<p>End Sub<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A sparkline is a miniature line graph that shows trending information over a time period. It is designed to be shown next to the name of a data type in a table of data to show a quick &#8220;at a glance&#8221; reference of how the data is trending over time. Microsoft is introducing sparklines as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,15],"tags":[18,17],"class_list":["post-670","post","type-post","status-publish","format-standard","hentry","category-office","category-technical-tips","tag-excel","tag-sparkline"],"_links":{"self":[{"href":"http:\/\/blog.tomleslie.ca\/index.php?rest_route=\/wp\/v2\/posts\/670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.tomleslie.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.tomleslie.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.tomleslie.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.tomleslie.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=670"}],"version-history":[{"count":7,"href":"http:\/\/blog.tomleslie.ca\/index.php?rest_route=\/wp\/v2\/posts\/670\/revisions"}],"predecessor-version":[{"id":676,"href":"http:\/\/blog.tomleslie.ca\/index.php?rest_route=\/wp\/v2\/posts\/670\/revisions\/676"}],"wp:attachment":[{"href":"http:\/\/blog.tomleslie.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.tomleslie.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=670"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.tomleslie.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}