Automatically create a set of charts from a block of data

PierreBenko

New Member
Joined
Jul 12, 2012
Messages
9
Hello!

I have a quite big database as a result of a study in Excel 2010. There is a special block of data for every participant in the study, including 16 data series in 16 lines. I have to make a unique chart for every line.


One block is bulid up in a way, that every row is the input for a chart, with the exception of one "spacer" row, which is empty.


Name
1 2 3 4 ...
A
B
C


D
E
F


For example in the sample above, there should be 6 line-charts (A, B, C, D, E, F) next to the block.


Can a macro be written, so I can automatically create the chart-set by selecting the block's upper left cell and running the macro?
Or is there any easier method?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Could you provide a sample of the data as well as the chart that is produced by the data
 
Upvote 0
These Macros create all the Charts based on the data as povided in your example

Code:
Option Explicit

Sub CreateChart()
    Dim RowNo As Integer
    Dim Ws As Worksheet
    Dim strRng As String
    
    Dim RowBase As Integer
    Dim ColOffset As Integer
    
    Set Ws = ThisWorkbook.Worksheets(1)
    Ws.Range("A1").Select
    
    Dim ChtObj As ChartObject
    For Each ChtObj In Ws.ChartObjects
        ChtObj.Delete
    Next
    
    RowBase = 4
    For RowNo = 5 To Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row
        If Len(Trim(Ws.Cells(RowNo, "B"))) = 0 Then
            RowBase = RowNo
            ColOffset = 0
        Else
            strRng = "$B$" & RowNo & ":$G$" & RowNo
            
            Set ChtObj = Ws.ChartObjects.Add(100, 30, 400, 250)
            ChtObj.Chart.ChartWizard Source:=Ws.Range(strRng), _
                gallery:=xlLine, _
                Title:=Range("$A$" & RowNo), _
                HasLegend:=False
                
            Call CoverRng(Ws, ChtObj, RowBase, ColOffset)
            ColOffset = ColOffset + 1
        End If
    Next RowNo
    MsgBox "Complete"
End Sub

Function CoverRng(Ws As Worksheet, ChtObj As ChartObject, ByVal RowBase, ByVal ColOffset As Integer)
    Dim RngToCover As Range
    
    Const H As Integer = 7
    Const W As Integer = 5
    
    ColOffset = ColOffset * H + 8
    Set RngToCover = Ws.Range(Cells(RowBase, ColOffset), Cells(RowBase + H, ColOffset + W))
    ChtObj.Height = RngToCover.Height
    ChtObj.Width = RngToCover.Width
    ChtObj.Top = RngToCover.Top
    ChtObj.Left = RngToCover.Left
    
    
End Function
 
Upvote 0

Forum statistics

Threads
1,215,633
Messages
6,125,925
Members
449,274
Latest member
mrcsbenson

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top