Solved

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

Posted on 2014-01-23
2
177 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 50

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

756 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