One effective way to identify the next page break in an Excel workbook using VBA is to use the Next property of the HPageBreaks or VPageBreaks object. The Next property returns the location of the next page break after a specified cell.
For example, the following code identifies the next vertical page break after cell A1:
Dim nextVB As Range
Set nextVB = ActiveSheet.VPageBreaks(1).Location.Offset(1)
The code sets the nextVB variable to the cell one row below the location of the first vertical page break on the active sheet. This can be useful for determining where to place data on the page to ensure it appears on the correct side of the next page break.
Similarly, the following code identifies the next horizontal page break after cell A1:
Dim nextHB As Range
Set nextHB = ActiveSheet.HPageBreaks(1).Location.Offset(, 1)
The code sets the nextHB variable to the cell one column to the right of the location of the first horizontal page break on the active sheet. This can be useful for determining where to place data on the page to ensure it appears below the next page break.
Note that the Next property may return an error if there are no more page breaks after the specified cell. It is important to handle this possibility in your code.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-10-05 11:00:00 +0000
Seen: 10 times
Last updated: May 11 '22