VBA for multiple charts from multiple ranges

LaterPater

New Member
Joined
Oct 5, 2017
Messages
1
Hello everyone!

I have a question about making charts with macros, on which I am sort of noob. I have tried to find the answer by looking at similar problems and do have an idea about the code, it's just hard to apply everything to my own problem. I would like to follow a course to learn more about macro's, but I am kind of in a hurry.

In the HTML (bottom of post) there is a table from which I want to create graphs, I would like to make a chart for every company in the table (this is a shortened file, in the original file I have about 250 companies).

The data is dynamic: the number of companies will change, the company names will change, the series data will change and the dates will change. The STAFF type will usually be the same, but when I want to add a staff type now it means i will have to reselect the data for each chart. The company names will be generated in the tables with a pivot table. Please note that the first chart will always refer to the first company and so on, although the company itself will change.

What I would like to do is have a macro that will generate a chart for each company on the worksheet 'Charts'. The macro will have to select the data for the first chart ($D$1:$AA$8), make the chart, then 'offset' the range 8 cells down to $D$9:$AA$17 and repeat the LOOP until the companyname cell value is 0 (my tables generate a 0 after the last company).

Basically, I hope it is possible to specify the layout and other settings for the graphs as well (I have a template for the chart that could be applied). Also it would be helpful if it places the charts in the right position, I would like to have three charts of similar size on the same place on each page. (first 3 rows empty, then chart, 1 row empty, chart, 1 row empty, chart and then on the following pages exactly the same)

I have one file with all the charts already there, so a solution to change the data series/layout for existing charts could also work.

I hope someone can help me with this, as I have changed all the graphs manually before this and I can tell you that it is not a nice job to do. Thanks!

Hugo


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
117Company1#N/B42899#N/B#N/B#N/B#N/B#N/B#N/B42906#N/B42908#N/B#N/B#N/B42912#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B
216Company1STAFF100000000000000000000000
315Company1STAFF200000000000000000000000
414Company1STAFF300000000000000000000000
513Company1STAFF400000000000000000000000
612Company1STAFF500000000000000000000000
711Company1STAFF601000000101000100000000
810Company1TOTAL01000000101000100000000
927Company2#N/B#N/B#N/B42901#N/B#N/B#N/B#N/B#N/B4290742908#N/B#N/B#N/B#N/B4291342914#N/B#N/B#N/B#N/B#N/B#N/B
1026Company2STAFF100000000000000000000000
1125Company2STAFF200020000011000031000000
1224Company2STAFF300000000000000000000000
1323Company2STAFF400000000000000000000000
1422Company2STAFF500000000000000000000000
1521Company2STAFF600000000000000000000000
1620Company2TOTAL00020000011000031000000
1737Company3#N/B#N/B4290042901#N/B#N/B#N/B#N/B#N/B42907#N/B#N/B#N/B#N/B#N/B4291342914#N/B#N/B#N/B#N/B#N/B#N/B
1836Company3STAFF100000000000000001000000
1935Company3STAFF200200000000000002000000
2034Company3STAFF300000000000000000000000
2133Company3STAFF400210000010000021000000
2232Company3STAFF500200000000000000000000
2331Company3STAFF600200000000000000000000
2430Company3TOTAL00810000010000024000000
2547Company4#N/B#N/B42900#N/B#N/B#N/B#N/B42905#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B
2646Company4STAFF100000000000000000000000
2745Company4STAFF200000000000000000000000
2844Company4STAFF300000000000000000000000
2943Company4STAFF400100001000000000000000
3042Company4STAFF500000000000000000000000
3141Company4STAFF600000000000000000000000
3240Company4TOTAL00100001000000000000000
3357Company5#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B42909#N/B#N/B42912#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B
3456Company5STAFF100000000000000000000000
3555Company5STAFF200000000000000000000000
3654Company5STAFF300000000000100100000000
3753Company5STAFF400000000000000000000000
3852Company5STAFF500000000000000000000000
3951Company5STAFF600000000000000000000000
4050Company5TOTAL00000000000100100000000
4167Company6#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B42913#N/B#N/B#N/B#N/B#N/B#N/B#N/B
4266Company6STAFF100000000000000000000000
4365Company6STAFF200000000000000000000000
4464Company6STAFF300000000000000000000000
4563Company6STAFF400000000000000010000000
4662Company6STAFF500000000000000000000000
4761Company6STAFF600000000000000000000000
4860Company6TOTAL00000000000000010000000
4977Company7#N/B4289942900#N/B429024290342904429054290642907429084290942910#N/B429124291342914#N/B#N/B#N/B#N/B#N/B#N/B
5076Company7STAFF1000000000000000102000000
5175Company7STAFF200104013001210034000000
5274Company7STAFF300000000010000000000000
5373Company7STAFF401003223345730421000000
5472Company7STAFF500000001301100011000000
5571Company7STAFF600102212121100232000000
5670Company7TOTAL01209449778114061910000000
5787Company8#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B42915#N/B#N/B#N/B#N/B#N/B
5886Company8STAFF100000000000000000000000
5985Company8STAFF200000000000000000000000
6084Company8STAFF300000000000000000000000
6183Company8STAFF400000000000000000000000
6282Company8STAFF500000000000000000100000
6381Company8STAFF600000000000000000000000
6480Company8TOTAL00000000000000000100000
6597Company942898#N/B#N/B42901#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B42910#N/B42912#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B
6696Company9STAFF100000000000000000000000
6795Company9STAFF200000000000000000000000
6894Company9STAFF310000000000000000000000
6993Company9STAFF400000000000000100000000
7092Company9STAFF520010000000010000000000
7191Company9STAFF600000000000000000000000
7290Company9TOTAL30010000000010100000000
73107Company10#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B42907#N/B42909#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B
74106Company10STAFF100000000000000000000000
75105Company10STAFF200000000000000000000000
76104Company10STAFF300000000000000000000000
77103Company10STAFF400000000010000000000000
78102Company10STAFF500000000000000000000000
79101Company10STAFF600000000000200000000000
80100Company10TOTAL00000000010200000000000
81117Company114289842899429004290142902#N/B#N/B4290542906429074290842909#N/B#N/B42912429134291442915#N/B#N/B#N/B#N/B#N/B
82116Company11STAFF100000000000000000200000
83115Company11STAFF212022001200000142300000
84114Company11STAFF300000000000000000000000
85113Company11STAFF400000000010100200000000
86112Company11STAFF5012210027811100410000000
87111Company11STAFF600010000262200200200000
88110Company11TOTAL13253003111531400952700000
891270#N/B#N/B#N/B#N/B#N/B#N/B#N/B42905#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B#N/B
901260STAFF100000000000000000000000
911250STAFF200000000000000000000000
921240STAFF300000000000000000000000
931230STAFF400000000000000000000000
941220STAFF500000001000000000000000
951210STAFF600000000000000000000000
961200TOTAL00000001000000000000000
Data
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Welcome to the Board


Code:
Sub MultipleCharts()
Dim r As Range, i%, ch, co As ChartObject
Const safety = 20
Sheets("Table").Activate
i = 0: Set r = [d1:aa8]
Do While i < safety And r.Cells(1, 1).Offset(, -1) <> 0
    Set ch = ActiveSheet.Shapes.AddChart2(216, xlBarClustered)
    ch.Chart.SetSourceData r
    ch.Chart.Location xlLocationAsObject, "Charts"
    Set r = r.Offset(8)
    i = i + 1
Loop
Sheets("Charts").Activate
Set r = [b4]: i = 1
For Each co In Sheets("Charts").ChartObjects
    co.Left = r.Left:            co.Top = r.Top
    co.Width = [b2:h2].Width:    co.Height = [a4:a15].Height
    co.Chart.ApplyChartTemplate _
    ("C:\Users\Eddie\AppData\Roaming\Microsoft\Templates\Charts\columns.crtx")
    Set r = r.Offset(IIf(i Mod 3 = 0, -26, 13), IIf(i Mod 3 = 0, 10, 0))
    i = i + 1
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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