count the number of rows to first page break

dantheman9

Board Regular
Joined
Feb 5, 2011
Messages
175
Hi,

can someone help modify the below code to count the row of the first page break only?

Code:
 For i = 1 To ActiveSheet.HPageBreaks.Count
  rowno = ActiveSheet.HPageBreaks(i).Location.Row
  Next


thanks

Dan
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Just a guess

Rich (BB code):
rowno = ActiveSheet.HPageBreaks(1).Location.Row
 
Upvote 0
thanks Jason,

seems to work now and again. I think I have other issues with the code.
Basily I need to work out the location of the first Page break which is set after formating the page forcing the page to be 1 page wide max.
Once the scaling is done, and i have the row location of the first page break, then all other sheets will be formated to insure best use of the page, and avoid data tables ending up been split over two pages;

Code:
 With ActiveSheet.PageSetup
        .PrintTitleRows = ""
        .PrintTitleColumns = ""
    End With
    ActiveSheet.PageSetup.PrintArea = ""
    With ActiveSheet.PageSetup
        .LeftHeader = "&G"
        .CenterHeader = "&""Bold""&14&U" & eventname & Chr(10) & eventname1 & Chr(10) & Chr(10) & "&G"
        .RightHeader = "&G"
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = "&7&P"
        .LeftMargin = Application.InchesToPoints(0.708661417322835)
        .RightMargin = Application.InchesToPoints(0.708661417322835)
        .TopMargin = Application.InchesToPoints(1.2)
        .BottomMargin = Application.InchesToPoints(0.748031496062992)
        .HeaderMargin = Application.InchesToPoints(0.3)
        .FooterMargin = Application.InchesToPoints(0.31496062992126)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .CenterHorizontally = True
        .CenterVertically = False
        .Orientation = xlPortrait
        .Draft = False
        .PaperSize = xlPaperA4
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .ScaleWithDocHeaderFooter = False
        .AlignMarginsHeaderFooter = True
        .EvenPage.LeftHeader.Text = ""
        .EvenPage.CenterHeader.Text = ""
        .EvenPage.RightHeader.Text = ""
        .EvenPage.LeftFooter.Text = ""
        .EvenPage.CenterFooter.Text = ""
        .EvenPage.RightFooter.Text = ""
        .FirstPage.LeftHeader.Text = ""
        .FirstPage.CenterHeader.Text = ""
        .FirstPage.RightHeader.Text = ""
        .FirstPage.LeftFooter.Text = ""
        .FirstPage.CenterFooter.Text = ""
        .FirstPage.RightFooter.Text = ""
    End With
 
ActiveSheet.DisplayPageBreaks = True
 
    Dim rowno As Integer
 
[COLOR=red] rowno = ActiveSheet.HPageBreaks(1).Location.Row[/COLOR]
 
  
  If rowno < 47 Then
  rowno = 47
  End If
 
 
    PgSize = rowno   '  Assumes 49 rows per page
    Set rStart = Range("A3")
    lastrow = Cells(Rows.Count, 1).End(xlUp).Row
 
 
    Do
        Set TestCell = rStart.Offset(PgSize, 0)
        If Len(TestCell) = 0 Or Len(TestCell.Offset(-1, 0)) = 0 Then
                Set rEnd = TestCell.End(xlUp)
            Else
                Set rEnd = TestCell.End(xlUp).End(xlUp)
        End If
        ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=rEnd.Offset(1, 0)
        Set rStart = rEnd.Offset(1, 0)
 
    n = n + 1
    If n > 1000 Then Exit Sub   '  Escapes from an infinite loop if code fails
    Loop Until rStart.Row > lastrow - (rowno - 2)

Stil having issues at the red marked line!

Cheers

Dan
 
Upvote 0
Maybe the problem is coming from

Code:
If rowno < 47 Then
  rowno = 47
  End If
 
 
    PgSize = rowno   '  Assumes 49 rows per page

What if the assumption is incorrect?

Personally, when trying to print in that way I would loop throught the tables on the principle of select, fit to page, print, repeat.

But I'm not so sure something like that would meet your requirements, and if it did, you would still need a consistand way to identify the correct rows to start and end the print for each table.
 
Upvote 0
Hi Jason,

From some more googling it seems that in order for PageBreak location to work, the actual break needs to be in the active display window! (hence why it seemed to work sometimes after i looks about the sheet in break mode and then restarted the code).

Bit of a pain, But still! At least I know why its not working!

don't surpose you have a quick and easy method to view to the first break?
 
Upvote 0
I could be wrong, but think that setting zoom to false and using 'fit to pages' disables normal page breaks, possibly you could see the row number with a messagebox

Code:
msgbox activesheet.hpagebreaks(1).location.row
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,558
Members
452,928
Latest member
101blockchains

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