I'm writing a report using VBA. As part of this process, I calculate the number of lines I have 'printed' and after a certain number (MaxReportLinesPerPage), when I am pointing to a blank line, I insert a manual page break. When I look at the worksheet afterwards, the page breaks are not where I put them (or, to be more exact, they are not where Excel says it is putting them).
This is the portion of code that determines whether or not to insert a page break:
' See if we need to insert a page break
If PageLineCount > MaxReportLinesPerPage Then
msg = "Page Break!" & vbCrLf
msg = msg & "PageLineCount=" & PageLineCount & vbCrLf & vbTab
msg = msg & "ReportRange=" & ReportRange.Address & vbCrLf & vbTab
MsgBox msg
ReportRange.PageBreak = xlManual
PageLineCount = 0
PrintComponentHeader = True
You will note that each time I do this I notify the user (via a msgbox) of:
PageLineCount
ReportRange.Address
The sequence of msgbox texts has the following variable values:
PageLineCount = 28
ReportRange.Address=$A$32
PageLineCount = 28
ReportRange.Address=$A$60
PageLineCount = 28
ReportRange.Address=$A$88
PageLineCount = 28
ReportRange.Address=$A$116
and so on
The ranges are going up by 28 lines each (which is what am hoping for) but the first page break occurs one line earlier than I am expecting.
Note that in every instance, I am placing 28 lines on the report. The first line is set at $A$4, which explains the fixed offset between the number of lines and the Range address.
(Each block of report lines is at least 3 lines of data, and possibly more, with a blank line between blocks. At present there are never more than three lines of data in a block.)
My core question is:
Why, when Excel reports that it is at $A32, and I insert a page break, does the page break actually show up just before $A$26? This is the only instance of inserting a pagebreak in the report procedure, so it does seem like Excel is not doing what it claims to do!?
MaxLinesPerReportRange is set to a value which should allow a block of lines to be printed, even if we have already reached the limit
Until I can answer that question, I can't make any progress to managing the more exact placement of report lines on the worksheet.
I know the answer is staring me in the face, but I can't see it when it's that close;-)
Tony
PS The spreadsheet is too large to upload, and it needs most of the worksheets to create the report.
Bookmarks