I am tracking rows in my worksheet and inserting a horizontal page break when the next "block" of data happens to spill over into another page. I track the cell before the "block" is entered so I know where I want to set a page break. This all seems to work fine until I hit the situation where an Excel-generated page break is on the bottom of the row that I want to set a top-of-the-row page break. It ignores the command that was used several times before hitting this situation. Any subsequent attempts to enter page breaks are ignored as well. However, when I go back and look at my spreadsheet, page breaks have been inserted in rows that I had already passed by.
Here is the code that is being used. ??? Where do I enter the code?????
Sub UTY_Count_Page_Breaks()
Stop
' ActiveSheet.Range("AA" & ActiveSheet.UsedRange.Rows.Count) = ""
ActiveSheet.Range("A" & ActiveSheet.UsedRange.Rows.Count).Select
' MsgBox ActiveSheet.HPageBreaks.Count 'Count For Next on HPageBreaks.Count
MsgBox CountOfBreaks & "/" & ActiveSheet.HPageBreaks.Count
If ActiveSheet.HPageBreaks.Count > 0 Then
' The following command is not working. Previously I could display the address
' ActiveSheet.HPageBreaks(ActiveSheet.HPageBreaks.Count).Location.Address 'Gives Range/Address of PageBreak 1 - But is not working at the moment.
End If
If ActiveSheet.HPageBreaks.Count > CountOfBreaks Then
CountOfBreaks = ActiveSheet.HPageBreaks.Count
Debug.Print Control_Last_Logical_PageBreak
ActiveSheet.HPageBreaks.Add before:=Rows(Int(Right(Control_Last_Logical_PageBreak, 2)))
' CountOfBreaks = CountOfBreaks + 1
End If
End Sub
I hope that I did that correctly.
Control_Last_Logical_PageBreak contains the address of the last row when I would want to logically insert a page break. I have come through 25 logical page breaks while building the worksheet, reading from a CSV file opened within the app. The VBA Code tracks the proper row address correctly each time. During that time I have had 2 or 3 successful page break insertions whenever the ActiveSheet.HPageBreaks.Count exceeds the CountOfBreaks.
I am not sure that I have explained this so that it can be understood, but I hope so. I went through several pages about page breaks but did not find anything that seemed to fit my situation. Any help would be greatly appreciated.
Note: I did not go back through the already processed data to see if a page break was inserted elsewhere in the document when the command failed after it initially failed where there was a bottom page break on the same row that I attempted to set a top page break.
I added a snippet of the spreadsheet. There are two incorrectly placed page breaks in the image. Only the page break at the top of Row 44 is valid (or what I expect) The other two came from"out of the blue" but probably were added when the insert page break began failing. As the image shows, line 44 and line 45 page breaks create a blank page and 41 and 43 create a page with little information on the page.
Excel added the break between 144 and 145. I attempted to set a top-of-row break but as you can see it did not take. I do not know if this page break is top-of-row or bottom-of-row.
After that initial failure, the insert page break no longer functioned properly. I attempted to set the top-of-row page break on line 192. As you can see, I was not successful.
Thanks for your help,
Dan Foltz
Here is the code that is being used. ??? Where do I enter the code?????
Sub UTY_Count_Page_Breaks()
Stop
' ActiveSheet.Range("AA" & ActiveSheet.UsedRange.Rows.Count) = ""
ActiveSheet.Range("A" & ActiveSheet.UsedRange.Rows.Count).Select
' MsgBox ActiveSheet.HPageBreaks.Count 'Count For Next on HPageBreaks.Count
MsgBox CountOfBreaks & "/" & ActiveSheet.HPageBreaks.Count
If ActiveSheet.HPageBreaks.Count > 0 Then
' The following command is not working. Previously I could display the address
' ActiveSheet.HPageBreaks(ActiveSheet.HPageBreaks.Count).Location.Address 'Gives Range/Address of PageBreak 1 - But is not working at the moment.
End If
If ActiveSheet.HPageBreaks.Count > CountOfBreaks Then
CountOfBreaks = ActiveSheet.HPageBreaks.Count
Debug.Print Control_Last_Logical_PageBreak
ActiveSheet.HPageBreaks.Add before:=Rows(Int(Right(Control_Last_Logical_PageBreak, 2)))
' CountOfBreaks = CountOfBreaks + 1
End If
End Sub
I hope that I did that correctly.
Control_Last_Logical_PageBreak contains the address of the last row when I would want to logically insert a page break. I have come through 25 logical page breaks while building the worksheet, reading from a CSV file opened within the app. The VBA Code tracks the proper row address correctly each time. During that time I have had 2 or 3 successful page break insertions whenever the ActiveSheet.HPageBreaks.Count exceeds the CountOfBreaks.
I am not sure that I have explained this so that it can be understood, but I hope so. I went through several pages about page breaks but did not find anything that seemed to fit my situation. Any help would be greatly appreciated.
Note: I did not go back through the already processed data to see if a page break was inserted elsewhere in the document when the command failed after it initially failed where there was a bottom page break on the same row that I attempted to set a top page break.
I added a snippet of the spreadsheet. There are two incorrectly placed page breaks in the image. Only the page break at the top of Row 44 is valid (or what I expect) The other two came from"out of the blue" but probably were added when the insert page break began failing. As the image shows, line 44 and line 45 page breaks create a blank page and 41 and 43 create a page with little information on the page.
Excel added the break between 144 and 145. I attempted to set a top-of-row break but as you can see it did not take. I do not know if this page break is top-of-row or bottom-of-row.
After that initial failure, the insert page break no longer functioned properly. I attempted to set the top-of-row page break on line 192. As you can see, I was not successful.
Thanks for your help,
Dan Foltz