Farscape2020
New Member
- Joined
- Mar 22, 2020
- Messages
- 17
- Office Version
- 365
- Platform
- 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).
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?
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?