Loop through pages

Roderick_E

Well-known Member
Joined
Oct 13, 2007
Messages
2,051
I have a worksheet that I'm currently viewing as page break.
I want to loop through all print-area pages and on each page determine how many chart objects are on each page.
Any suggestions? -- VBA obviously. Thanks
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try this code as a starting point.
Code:
Public Sub Count_Charts_On_Each_Page()

    Dim page As Long
    Dim chartObj As ChartObject
    Dim HPageBreakStartRow As Long
    Dim numChartsOnPage As Long
    
    HPageBreakStartRow = 1
    With ActiveWorkbook.ActiveSheet
        For page = 1 To .HPageBreaks.Count
            numChartsOnPage = 0
            'Debug.Print page, HpageBreakStartRow, .HPageBreaks(page).Location.Row
            For Each chartObj In .ChartObjects
                'Debug.Print chartObj.BottomRightCell.Address
                If chartObj.TopLeftCell.Row >= HPageBreakStartRow And chartObj.BottomRightCell.Row < .HPageBreaks(page).Location.Row Then
                    numChartsOnPage = numChartsOnPage + 1
                    'Debug.Print chartObj.Name & " is on page " & page
                End If
            Next
            Debug.Print numChartsOnPage & " charts on page " & page
            HPageBreakStartRow = .HPageBreaks(page).Location.Row
        Next
    End With

End Sub
 
Upvote 0
Wow, this is great thank for the feedback and code. It doesn't get the last page since it is counting page breaks.
Maybe thinking about reworking with

With ActiveWorkbook.ActiveSheet
For page = 1 To .Pagesetup.pages.count
 
Upvote 0
Hmmm, maybe I'm going about this wrong. What I'm trying to accomplish is automatically align charts in a pagebreak section. If 1 chart then take up entire pagebreak section. If 2 charts, place then over each other horizontally taking up entire pagebreak section. If 3 charts, 2 on top next to each other and 3rd on bottom. If 4 then 4 evenly spaced squares within the pagebreak section.

Left is easy enough to determine if there is only 1 chart but the rest is very dynamic. Where the top and bottom are and such.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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