Checking for Page Breaks Multiple Times

Farscape2020

New Member
Joined
Mar 22, 2020
Messages
17
Office Version
  1. 365
Platform
  1. Windows
I have created list of data that is divided into separate sections with their own title. For the first section, if the data crosses a page break, I would like to insert some rows and add the headings on top of the page. For the second and third section, if it crosses a page break, I would like to move it to the next page. To detect the horizontal page break, I am using the following code (found on this discussion board).

VBA Code:
    Dim HPB As HPageBreak
    Dim pageArray() As Integer
    Dim i As Integer

    i = 0

    For Each HPB In ws.HPageBreaks
        ReDim Preserve pageArray(i)
        pageArray(i) = Right(HPB.Location.Address, Len(HPB.Location.Address) - InStr(2, CStr(HPB.Location.Address), "$", vbTextCompare))
        i = i + 1
    Next

This works well for the first section. However, because the inserted titles has wrapped text which takes up muliple lines, this changes where the page breaks are located. For instance, when I initally run this loop, it states the second horizontal page break is at row 62. When I insert the title row, the page break is moved to row 61. In between fomatting sections, I would like to run this loop again to revaluate where the page breaks are located to make the formatting more accurate. However, when I run this loop, I seem to "randomly" receive the error "run-time error '9': Subscript out of range" and I receive this error each time I run this loop in between formatting sections. When I say I "randomly" receive this error, sometimes it occrus near the beginning, middle, or end when creating my sheets.

Would someone be able guide me to be able to check, multiple times, where the horizontal page breaks are located or or better method to achieve my desired outcome?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Would someone be able guide me to be able to check, multiple times, where the horizontal page breaks are located or or better method to achieve my desired outcome?
I don't quite understand what you're trying to do, but let's start with getting where the horizontal page breaks are located & load it into an array. Something like this:
VBA Code:
Dim HPB As HPageBreak
    tx = ""
    For Each HPB In ActiveSheet.HPageBreaks
        tx = tx & "," & HPB.Location.Row
    Next
    ary = Split(Mid(tx, 2), ",")

The array "ary" will capture every horizontal page breaks row.

It would be better if you could upload a sample workbook (without sensitive data) to a file-sharing site like Dropbox.com or Google Drive, and then share the link here. Also, ensure that the link is accessible to anyone.
You can set up an example with two sheets:
Sheet1 contains your existing data.
Sheet2 contains the data as expected.
 
Upvote 0
I have created list of data that is divided into separate sections with their own title. For the first section, if the data crosses a page break, I would like to insert some rows and add the headings on top of the page. For the second and third section, if it crosses a page break, I would like to move it to the next page. To detect the horizontal page break, I am using the following code (found on this discussion board).

VBA Code:
    Dim HPB As HPageBreak
    Dim pageArray() As Integer
    Dim i As Integer

    i = 0

    For Each HPB In ws.HPageBreaks
        ReDim Preserve pageArray(i)
        pageArray(i) = Right(HPB.Location.Address, Len(HPB.Location.Address) - InStr(2, CStr(HPB.Location.Address), "$", vbTextCompare))
        i = i + 1
    Next

This works well for the first section. However, because the inserted titles has wrapped text which takes up muliple lines, this changes where the page breaks are located. For instance, when I initally run this loop, it states the second horizontal page break is at row 62. When I insert the title row, the page break is moved to row 61. In between fomatting sections, I would like to run this loop again to revaluate where the page breaks are located to make the formatting more accurate. However, when I run this loop, I seem to "randomly" receive the error "run-time error '9': Subscript out of range" and I receive this error each time I run this loop in between formatting sections. When I say I "randomly" receive this error, sometimes it occrus near the beginning, middle, or end when creating my sheets.

Would someone be able guide me to be able to check, multiple times, where the horizontal page breaks are located or or better method to achieve my desired outcome?
I don't get exactly what "Farscape2020"'s desired outcome is but in my case I wanted page breaks only in blank rows this is the code i used this should be easy to adjust to your desired outcome:

Sub SetPageBreaksOnBlankRows()
Dim ws As Worksheet
Dim PBr As Long
Dim AD As String
Dim PBS As HPageBreaks 'HPageBreaks for Horizontal use VPageBreaks for vertical
Dim HPB As HPageBreak
Set ws = ActiveSheet
Set PBS = ws.HPageBreaks

' this selects the last row see here Page breaks, search manual PageBreaks & time error 9: “Subscript out of Range”
ws.Cells.Find(What:="*", _
after:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Select

For Each HPB In PBS
PBr = HPB.Location.row
'test if page break location row is not blank and 1 row above is not blank then move to the first blank row above it
If Application.WorksheetFunction.CountA(ws.Rows(PBr)) > 0 And Application.WorksheetFunction.CountA(ws.Rows(PBr - 1)) > 0 Then
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ws.Cells(PBr, 1).CurrentRegion
End If
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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