cellMatrix.net
Spreadsheet Modeling and Related Topics

About

Formulas, Charts, and Models Created with Microsoft Excel.
More . . .

Statistics

  • Total Entries - 102
  • Current Viewers - 13

Categories

Recent Comments

Syndicate

Validate

Align Charts to Worksheet Grid Post 2

As a follow-up to Align Charts to Worksheet Grid Post 1, the following procedure assigns a name, sizes, and aligns all of the embedded charts on a single worksheet. The key to the procedure is the location (range address) of the cell at the top left side of the chart.

Sub AlignCharts()

    Dim Cht As ChartObject

    For Each Cht In ActiveSheet.ChartObjects
        'Note: Naming the chart is optional.
        Cht.Parent.Name = Cht.TopLeftCell.Offset(-1, 0).Value
        Cht.Top = Cht.TopLeftCell.Top
        Cht.Left = Cht.TopLeftCell.Left
        Cht.Height = 114.75
        Cht.Width = 192
    Next Cht

End Sub

Given four embedded charts on a single sheet, the output looks like this:

image

Each chart is assigned the name of the value in the cell just above the top left cell. Each chart is aligned to the top-left cell and sized as per the height and width assignments.

With this procedure you don't have to worry about naming the charts first or building loops to place them. Just create a new chart, move it to the top-left cell location of your choice, add a name for the chart above the top-left cell, and run the macro.


Add Labels to Chart Data Points

In Excel there is no way to automatically attach text labels to data points within a chart. However, Microsoft has provided a VBA procedure to do so via it's Knowledge Base Article 213750.

The macro can be shortened to the following. To run the macro, first copy it to a standard module. Next, activate the chart and run it.

Sub AddLabels()
    Application.ScreenUpdating = False
    Dim Rng As Range
    Dim Cht As Chart
    Dim i As Integer
    Set Cht = ActiveChart
    Set Rng = ActiveSheet.Range("A1:A10")
    Cht.SeriesCollection(1).ApplyDataLabels Type:=xlDataLabelsShowLabel
    Pts = Cht.SeriesCollection(1).Points.Count
    For i = 1 To Pts
        Cht.SeriesCollection(1).Points(i).DataLabel.Text = Rng(i)
    Next i
    Application.ScreenUpdating = True
End Sub

To add the functionality of automatically updating the data labels whenever the cells are changed, the macro can be slightly modified to the following:

Replace

Cht.SeriesCollection(1).Points(i).DataLabel.Text = Rng(i)

with

Cht.SeriesCollection(1).Points(i).DataLabel.Text = "=" & Rng(i).Address(, , xlR1C1, True)

Finally, if you don't feel like fooling around with code, there are two free utilities available that allow you to attach text labels to data points within a chart:


Breakeven Chart with Dynamic Label

A recent post to the Microsoft Excel Charting newsgroup asks "I have a chart where two lines intersect at the breakeven point. How can I show a dynamic label at the point of intersection that describes the breakeven point?"

A simple solution is to add a text box in the chart. Make the source of that text box a worksheet cell that contains the data that you want to show.

For example, the model below provides the data needed to create a breakeven chart.

image

Based on the information above, a combination Line - XY Scatter Chart can be created to show the breakeven point.

image

When changes are made to the model, the label at the top of the chart will always reflect those changes. To create the label, first decide what worksheet cell you want the label to tie to. In this case the label ties to cell C25. Within that cell you can reference what is already there or create a phase that you want to appear. In this example the phrase "BE Units = 152.16 and BE Cost = $4,868" is returned by using the following formula in cell C25:

="BE Units = "&TEXT(F5,"##.##")&" and "&"BE Cost = "&TEXT(G5,"$#,###")

Finally, add the label to the chart. To do so, select the chart (select any element except a text item). Click in the formula bar, then type the number if it's static or type = and click on the cell with the value (no need to create the textbox first). Press enter and a textbox should appear in the middle of the chart. This will put a textbox in the chart.

Note: Instructions for adding the text box to the chart was provided by Jon Peltier of Peltier Technical Services via a past post to the Microsoft Excel Charting Newsgroup.





Dynamic Chart - Show Range Between Values

The following example uses a combination of defined names and array formulas to dynamically chart a range between a high and low value. In the screenshot below, data is entered into the yellow shaded cells. The chart source is within the white grid range E3:F22.

image

The first step to creating the chart is to build four named ranges. Three of the four named ranges will act as components of the array formulas that make up the chart source. Each of the formulas can be viewed in the screenshots of the named range dialog boxes below:

The first named range is called "Data":

image

The second named range is called "Labels":

image

The third named range is called "List":

image

The fourth and final named range is called "Values":

image

Two array formulas make up the chart source. To enter an array formula, select the entire range, enter the formula into the formula bar, and then hit the CONTROL - SHIFT - ENTER keys at the same time.

The first array formula below covers the range E3:E22.

=IF(ISERR(SMALL(IF(Data=F3,ROW(Data)-MIN(ROW(Data))+1),COUNTIF($F$3:F3,F3)))," ",
INDEX(List,SMALL(IF(Data=F3,ROW(Data)-MIN(ROW(Data))+1),COUNTIF($F$3:F3,F3))))

The second array formula below covers the range F3:F22.

=IF(ISERR(SMALL(IF(Data>=$H$3,IF(Data<=$H$2,ROW(INDIRECT("1:"&ROWS(Data))))),
ROW(INDIRECT("1:"&ROWS(Data))))),"",INDEX(Data,SMALL(IF(Data>=$H$3,
IF(Data<=$H$2,ROW(INDIRECT("1:"&ROWS(Data))))),ROW(INDIRECT("1:"&ROWS(Data))))))

Finally, it's time to create the chart. Assuming that the file name is "RangeBetween.xls", the chart source dialog box looks like this:

image




Why Use Pie Charts?

I try to spend a few minutes each day in the Microsoft Excel Charting forum and it seems like each day someone has a question about pie charts. Today a question was brought up about overlapping data labels in a pie chart with 13 data points. Who cares about the labels . . . what can you see in a pie chart with 13 points? Why do people use pie charts?

This quote is brought to us courtesy of Juice Analytics. Coda Hale writes:

Piecharts are for middle management. Piecharts are the information visualization equivalent of a roofing hammer to the frontal lobe. They have no place in the world of grownups, and occupy the same semiotic space as short pants, a runny nose, and chocolate smeared on one’s face. They are as professional as a pair of assless chaps. Anyone who suggests their use should be instinctively slapped.

For the record, I avoid pie charts like the plague. I've never seen a case where a pie chart performed better than a bar chart or a simple data table.

Below are links to sources that explain how pie charts should and shouldn't be used. They also describe the data visualization problems associated with pie charts. Additional links would be appreciated.


In-Cell Charting

For the purposes of this post, "In-Cell" charts can be defined as very small charts, or chart pictures, that cover the height and width of one worksheet cell. Because In-Cell charts are so small, they can be very useful for dashboard reporting.

I've found that it's extremely difficult if not impossible to scale-down and manage one or more Excel charts to the size of a worksheet cell. However, it's not that difficult if the chart is converted to a picture, scaled down, and then positioned into the cell.

The simple report below provides an example of a series of charts scaled down to the size of worksheet cells. The objective of this report is to provide the current month's statistic and a high-level graphic showing the six month trend.

image

To create the report, I set up the data to be included in each In-Cell chart on the right side of the report outside of the page view. I then calculated a minimum and maximum for each row of data. The data set looks like this:

image

A single chart is created that uses one row of data as the six-month trend source. I named the chart "Cht1". A macro is used to loop through each row of data, update the chart, copy the chart as a picture, scale the picture down to the size of one worksheet cell, and then place the picture into the appropriate cell. The macro is below:

Sub BuildMicroCharts()

    Application.ScreenUpdating = False

    Dim Rng As Range
    Dim ChtRng As Range
    Dim ChtMax As Range
    Dim ChtMin As Range
    Dim Cht As ChartObject

    Set ChtRng = ActiveSheet.Range("I4:N4")
    Set ChtMax = Range("O4")
    Set ChtMin = Range("P4")
    Set Cht = ActiveSheet.ChartObjects("Cht1")

    For Each Rng In Range("F4:F11")

        ActiveSheet.ChartObjects("Cht1").Activate
        ActiveChart.SetSourceData Source:=ChtRng, PlotBy:=xlRows
        ActiveChart.Axes(xlValue).Select
        With ActiveChart.Axes(xlValue)
            .MaximumScale = ChtMax
            .MinimumScale = ChtMin
            .MajorUnit = (.MaximumScale - .MinimumScale) / 6
            .MinorUnit = (.MaximumScale - .MinimumScale) / 12
        End With

        Cht.CopyPicture Appearance:=xlPrinter, Format:=xlPicture
        Rng.Select
        ActiveSheet.Paste
        
        Selection.ShapeRange.LockAspectRatio = msoFalse
        Selection.ShapeRange.Height = 15
        Selection.ShapeRange.Width = 48

        Set Rng = Rng.Offset(1, 0)
        Set ChtRng = ChtRng.Offset(1, 0)
        Set ChtMax = ChtMax.Offset(1, 0)
        Set ChtMin = ChtMin.Offset(1, 0)

    Next Rng

    Range("A1").Select

    Application.ScreenUpdating = True

End Sub

To delete the series of In-Cell charts, I refered to Ron de Bruin's site which contains many examples of code showing how to delete shapes from a worksheet. The following code deletes each In-Cell chart from the worksheet (the shape type for each In-Cell chart is 13) while preserving the actual chart that serves as the picture source (the shape type for the picture source chart is 3).

Sub DeleteShapes()
    Dim Shp As Shape
    For Each Shp In ActiveSheet.Shapes
        If Shp.Type = 13 Then Shp.Delete
    Next Shp
End Sub

Finally, Ron also provides code that shows the Type numbers of all controls on your worksheet. That code allows you to differentiate between the Type numbers for chart pictures that should be deleted and the actual chart source which you don't want to delete. That code is below:

Sub ListAllObjectsActiveSheet()
    Dim NewSheet As Worksheet
    Dim MySheet As Worksheet
    Dim myshape As Shape
    Dim I As Long

    Set MySheet = ActiveSheet
    Set NewSheet = Worksheets.Add

    With NewSheet
        .Range("A1").Value = "Name"
        .Range("B1").Value = "Visible(-1) or Not Visible(0)"
        .Range("C1").Value = "Shape type"
        I = 2

        For Each myshape In MySheet.Shapes
            .Cells(I, 1).Value = myshape.Name
            .Cells(I, 2).Value = myshape.Visible
            .Cells(I, 3).Value = myshape.Type
            I = I + 1
        Next myshape

        .Range("A1:C1").Font.Bold = True
        .Columns.AutoFit
        .Range("A1:C" & Rows.Count).Sort Key1:=Range("C1"), _
                        Order1:=xlAscending, Header:=xlYes
    End With

End Sub

Using the VBA techniques above, you can quickly scale and position many In-Cell charts in a very short period of time.