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

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