Organize Layout of All Charts

deZine

New Member
Joined
May 6, 2009
Messages
22
Hello,
I was given VBA that reorganizes all the charts on a sheet by a certain number of charts wide. I would like to change it to be a particular number of charts tall instead.

For Example in the code below is set to be 7 charts wide, so if I have 14 charts, the grid will be 7 charts wide by 2 charts tall. It lays each one to the right of the previous chart until it reaches the "nColumns" then starts the next row of charts. This results in the charts being further away from their source column than I would like.
Right now it is laying them out in this order:
1 2 3 4 5 6 7
8 9 10 11 12 13 14

I would like it to set them up with the height defined as 3 tall and lay them out top to bottom until that number is reached and then start the next column of charts.
I am hoping to have it laid out in this order:
1 4 7 10 13
2 5 8 11 14
3 6 9 12

VBA Code:
'Change size and position of all charts
    Dim iChart As Long
    Dim nCharts As Long
    Dim dTop As Double
    Dim dLeft As Double
    Dim dHeight As Double
    Dim dWidth As Double
    Dim nColumns As Long

    dTop = 55      ' top of first row of charts
    dLeft = 212    ' left of first column of charts
    dHeight = 202  ' height of all charts
    dWidth = 431   ' width of all charts
    nColumns = 7   ' number of columns of charts
    nCharts = ActiveSheet.ChartObjects.Count

    For iChart = 1 To nCharts
        With ActiveSheet.ChartObjects(iChart)
            .Height = dHeight
            .Width = dWidth
            .Top = dTop + Int((iChart - 1) / nColumns) * dHeight
            .Left = dLeft + ((iChart - 1) Mod nColumns) * dWidth
        End With
    Next

Thanks so much for taking the time to read this and for any help!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try . . .

VBA Code:
'Change size and position of all charts
    Dim iChart As Long
    Dim nCharts As Long
    Dim dTop As Double
    Dim dLeft As Double
    Dim dHeight As Double
    Dim dWidth As Double
    Dim nRows As Long

    dTop = 55      ' top of first row of charts
    dLeft = 212    ' left of first column of charts
    dHeight = 202  ' height of all charts
    dWidth = 431   ' width of all charts
    nRows = 3   ' number of rows of charts
    nCharts = ActiveSheet.ChartObjects.Count

    For iChart = 1 To nCharts
        With ActiveSheet.ChartObjects(iChart)
            .Height = dHeight
            .Width = dWidth
            .Top = dTop + ((iChart - 1) Mod nRows) * dHeight
            .Left = dLeft + Int((iChart - 1) / nRows) * dWidth
        End With
    Next

Hope this helps!
 
Upvote 0
Solution
Hi Dominic,
Thanks so much for the rapid response and apologies for my delay in messaging back. That works exactly as I hoped. Thanks again and Happy holidays!
 
Upvote 0
That's great to hear, I'm glad I could help.

And Happy Holidays to you too as well.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,742
Members
448,989
Latest member
mariah3

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