Solved

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

Posted on 2014-01-23
2
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 51

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

737 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