cellmatrix.net

Auto-Sort Chart

This example allows a fixed range for inputs, automatically sorts the results, and charts those results. The input area and chart source look like this:

image

The formulas in column D calculate a very small number that is subtracted from the original. The purpose is to prevent possible duplication of entries. Below is the formula in cell D4.

=C4-ROW()/10^10

The formulas in column F sort the data in column D from highest to lowest value. Below is the formula in cell F4.

=LARGE($D$4:$D$13,ROWS($B$4:B4))

Finally, the formulas in column E utilize the INDEX and MATCH worksheet functions to perfrom a left-sided look-up on the data in column F. Below is the formula in cell E4.

=INDEX($B$4:$B$13,MATCH($F4,$D$4:$D$13,0))

The chart below provides the end-result.

image
Reference: An explanation of the LARGE sorting array formula was obtained from John Walkenbach's book titled Excel 2003 Formulas. The INDEX and MATCH functions are also explained in this book as well as at Debra Dalgleish's Contextures site.

Auto-Color Chart Bars Version 2

This next variation of the color-bars macro allows you to assign colors to the chart bars based on the background colors of selected cells. For example, assume the screenshot below:

image

The macro below loops through the department column and assigns a color to the bars based on that department.

Sub ColorBars()

    Application.ScreenUpdating = False

    Dim Rng As Range
    Dim Cnt As Integer
    Dim Color As Integer

    Cnt = 1

    For Each Rng In Range("B3:B10")
        Color = Rng.Interior.ColorIndex
        Set Pts = ActiveChart.SeriesCollection(1).Points(Cnt)
        Pts.Interior.ColorIndex = Color
        Cnt = Cnt + 1
    Next Rng

End Sub

The final product looks like the chart below.

image

Auto-Color Chart Bars Version 1

Recently at work I was asked to create a bar chart that showed various divisions grouped by departmental assigned colors. For example, there are four departments assigned - Internal Medicine, Surgery, Medicare Specialties, and Other. Within each of these departments are various divisions. The task was to show each division as a bar and color that bar according to what department they were assigned to.

This can be done fairly easily (and without code) using an individual column assigned to a department. The rows consist of divisions and the data is assigned to the cell where the division and department intersect. The problem with this approach is that the chart source consists of as many series as there are departments. It is also difficult to maintain the correct colored bars if the data needs to be sorted.

In an effort to keep things simple my thought was to use just one series of data and use a macro to color the bars associated with that series. As an example, the following screenshot shows a worksheet with three columns - department, division, and revenue.

image

The macro below loops through the department column and assigns a color to the bars based on that department.

Sub ColorBars()

    Application.ScreenUpdating = False

    Dim Rng As Range
    Dim Cnt As Integer

    Cnt = 1

    For Each Rng In Range("B3:B10")
        Set Pts = ActiveChart.SeriesCollection(1).Points(Cnt)
        If Rng.Value = "IM" Then
            Pts.Interior.ColorIndex = 24
        ElseIf Rng.Value = "SURG" Then
            Pts.Interior.ColorIndex = 15
        ElseIf Rng.Value = "MS" Then
            Pts.Interior.ColorIndex = 19
        ElseIf Rng.Value = "OTHER" Then
            Pts.Interior.ColorIndex = 35
        End If
        Cnt = Cnt + 1
    Next Rng
End Sub

The final product looks like the chart below.

image

Proportional Axis Calculations - Unit Conversions

A recent post to the Microsoft Excel Charting Discussion Group asks "how can I show pounds on the primary axis and kilograms on the secondary axis with tick marks that correspond to each other?" I'm thinking the chart should look something like the one below:

image

The solution requires calculating a primary and secondary axis that are proportional to each other. The illustration below shows those calculations:

image

Finally, I've found that the VBA procedure below is the easiest and most accurate method to set each axis. Add the procedure to a regular module, select the chart, and run the macro to set the axis.

Sub SetAxis()
    ActiveChart.Axes(xlValue).Select
    With ActiveChart.Axes(xlValue)
        .MaximumScale = Range("C26").Value
        .MinimumScale = Range("C28").Value
        .MajorUnit = Range("C29").Value
    End With
    ActiveChart.Axes(xlValue, xlSecondary).Select
    With ActiveChart.Axes(xlValue, xlSecondary)
        .MaximumScale = Range("D26").Value
        .MinimumScale = Range("D28").Value
        .MajorUnit = Range("D29").Value
    End With
    ActiveChart.Deselect
End Sub

Reference: The macro above is a variation of a procedure described at the Peltier Technical Services web site.

You end up with four series when you really only want to show two. At this point double-click on a series. In the Format Data Series dialog box, you can hide two of the four series via the Patterns tab.


Data Labels - Even Top Alignment

There may be times when you want to present a chart with labels aligned evenly on a horizontal plane. As an example, begin with the chart below:

image

The procedure below will loop through each point in the series and evenly position the data labels associated with those points.

Sub AlignDataLabels()
  Dim Cnt As Long
  Set Srs = ActiveChart.SeriesCollection(1)
  With Srs
    For Cnt = 1 To .Points.Count
        Srs.Points(Cnt).DataLabel.Top = 135
    Next
  End With
End Sub

The end-result looks like the chart below. All of the data labels are evenly positioned near the base of the bars.

image