Solved

How to add page break as query grows or shrinks (#2)

Posted on 2014-01-23
2
159 Views
Last Modified: 2014-02-10
I am using this macro:

Sub InsertHorizontalPageBreaks()
    Dim RowNbr As Long, ws As Worksheet
   
    Set ws = ActiveSheet
    ws.ResetAllPageBreaks
   
    For RowNbr = 1 To ws.UsedRange.Rows.Count
        If Left(ws.Cells(RowNbr, 1), 5) = "Race:" Then
            ws.Rows(RowNbr).PageBreak = xlPageBreakManual
        End If
    Next RowNbr
End Sub

It works 90% of the way. Say I have racers for race 1 on rows 2-5. On row 6, i have the next race # title. Then on row 7-10 in have racers for race 2. If I add racers to race 1, the page break stays above row 6, it does't move down to row 7. So that means all but one racer will be on one page, the last racer is going onto the top of the page with race 2, then below are the racers for race 2. How do i make it so the page brake follows right above the race #, as i add/delete racers?
0
Comment
Question by:brasiman
2 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39805720
Hi,

why not

Cleaning the other pagebreak at same time yout the new ones?

Sub InsertHorizontalPageBreaks()
    Dim RowNbr As Long, ws As Worksheet
    
    Set ws = ActiveSheet
    ws.ResetAllPageBreaks
    
    For RowNbr = 1 To ws.UsedRange.Rows.Count
        If Left(ws.Cells(RowNbr, 1), 5) = "Race:" Then
            ws.Rows(RowNbr).PageBreak = xlPageBreakManual
        Else
            ws.Rows(RowNbr).PageBreak = xlPageBreakNone
        End If
    Next RowNbr
End Sub

Open in new window

Regards
0
 

Author Comment

by:brasiman
ID: 39825521
Two questions:
1. I ran this and it worked, but i discovered when i have things added/deleted from my data, making the number of rows on the query go up and down, the page break isn't following my data up and down. Every time i refresh the query, do i have to run the marco? If so thats a pain!
2. I saved my spreadsheet, then when i went back into the macro, i got an error:
"Run-time error '1004': Unable to set the PageBreak property of the Range class.

I copied your macro exactly as is.

Any ideas on these two items?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now