cellMatrix.net

Spreadsheet Modeling and Related Topics

Creating Multiple Static Charts from an Excel Database

Periodically I have to present a report showing trends for each work unit within the practice that I work for. There are approximately 100 work units and the trend for each unit must be presented in chart form. For years we used a single workbook that contained an embedded chart for each unit. Unfortunately, because work units changed and employees changed jobs, this method proved to be time consuming and prone to error.

A more efficient process involves storing the data for each work unit in database form. In this example, a worksheet called "dBase" reflects the data for the work units as follows:

image

A second sheet, called "Model", contains a single chart and a chart source arranged as follows:

image

Based on the name of the unit in the "Model" tab - cell "F4", the data in the range "C7:N7" is updated via lookup formulas with the data from the "dBase" tab.

To create the report, a macro loops through the "dBase" tab - range "C5:C19". The unit name is recorded in cell "C5" above. Lookup formulas update the chart source based on the unit name. The updated chart from the "Model" tab is copied as a shape and then placed on a third sheet called "Rpt". The "Rpt" tab is what is presented.

The macro that builds the report is below:

Sub BuildReport()

    Application.ScreenUpdating = False

    Call DeleteShapes

    Dim Cht As Chart
    Dim Rng1 As Range
    Dim Rng2 As Range
    Dim Rng3 As Range

    Dim Count As Integer
    Dim ChartsAcross As Integer

    Set Rng2 = Sheets("Model").Range("F4")
    Set Rng3 = Sheets("Rpt").Range("C5")
    Set Cht = Sheets("Model").ChartObjects("Cht1").Chart

    ChartsAcross = 2

    For Each Rng1 In Sheets("dBase").Range("C5:C19")

        Rng2 = Rng1

        Cht.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
        With Rng3.Parent
            .Paste
            With .Shapes(.Shapes.Count)
                .Left = Rng3.Left
                .Top = Rng3.Top
            End With

            Rng3.Offset(-1, 0).Value = Rng2.Value
            Rng3.Offset(-1, 2).Value = "Chart " & Sheets("Rpt").Shapes.Count
            Rng3.Offset(-1, 2).HorizontalAlignment = xlRight
        
        End With

        If Sheets("Rpt").Shapes.Count Mod ChartsAcross = 0 Then
            Set Rng3 = Rng3.Offset(11, -4)
        Else
            Set Rng3 = Rng3.Offset(0, 4)
        End If

    Next Rng1

End Sub

Note that chart on the "Model" tab has been named "Cht1". You can find more about how to name an Excel chart at the Peltier Technical Services site.

The resulting report looks like this:

image

Note that the macro runs efficiently because it does not individually select a sheet, chart, or shape.

Letting a macro do the work has cut several hours off of the time it originally took to create the report. Errors are all but nonexistent.


Posted on Sunday, August 09, 2009 | Comments (1) | Permalink
Comments

There is obviously a lot to know about this.  There are some good points here.

Posted by GeneTinsley  on  10/09  at  04:55 PM
Page 1 of 1 pages

Comment Entry Form

User Information

Name: (Required)

E-Mail Address: (Optional)

Location: (Optional)

Web Site Address: (Optional)

Comment Formatting Reference
Add VBA Code:
<pre>some code here</pre>
Linking:
[url]http://www.example.com/[/url]
[url=http://www.example.com/]my site[/url]
Text Formatting
[size=4]Some larger text[/size]
[color=green]Some green text[/color]
[b]Some bold text[/b]
[i]Some italic text[/i]
Enter Comment Below

Personalization Options
Remember my personal information
Notify me of follow-up comments?

Next entry: Browser Compatibility

Previous entry: VBA to Load Named Ranges for Dynamic Charts

<< Back to main

Statistics

  • Total Entries - 136
  • Current Viewers - 25

Categories

Entries by Day

Sep - 2010
S M T W T F S
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2

Recent Comments

Syndicate