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
Solved

How to add page break as query grows or shrinks

Posted on 2014-01-23
11
262 Views
Last Modified: 2014-01-24
I have an access database and am pulling data into a spreadsheet through ODBC. I need to pull a list of all racers that have signed up for the race. This list of racers changes all the time as people sign up or remove themselves. As this list evolves in the Access database, as you know, this spreadsheet will update. When i print this list, I need the Race # (Row 4, 9, 14 in this example) to print on a new page with "Race: 65515" at the top as a header, same with the other two. So Race 65515 would start on pg 1, race 65516 would start on page 2 (unless the first race spills over onto a 2nd page, then race 65516 would start on page 3, etc). I tried putting the header "Race: 65516" after the names of race 65515, but as i add names to race 65515, it deletes my header as the first race list grows. I also tried putting a page break after each race names, but as i added names to each race, the page break stayed on that row.
Example.xlsx
0
Comment
Question by:brasiman
  • 7
  • 4
11 Comments
 
LVL 21

Expert Comment

by:Ejgil Hedegaard
ID: 39805099
Try this, it will remove all pagebreaks and insert new above rows starting with Race:

Remember to activate macros when file is opened
See example

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

Open in new window

Example-pagebreaks.xlsm
0
 

Author Comment

by:brasiman
ID: 39805112
Hi HGHOLT. Thanks for the quick reply! What if the race numbers don't have "Race: ####"? What if it is just the race number?
0
 

Author Comment

by:brasiman
ID: 39805144
Did you add page breaks yourself? Also, If you did, when i refresh my query, the breaks aren't adjusting by themselves. Also, when my list grows, it wipes out the 2nd and 3rd Race: ####. I couldn't get that coding to work.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 21

Accepted Solution

by:
Ejgil Hedegaard earned 500 total points
ID: 39805152
This checks if it is a number, or a string starting with a number.

Sub InsertHorizontalPageBreaksOnValues()
    Dim RowNbr As Long, ws As Worksheet
    
    Set ws = ActiveSheet
    ws.ResetAllPageBreaks
    
    For RowNbr = 1 To ws.UsedRange.Rows.Count
        If Val(ws.Cells(RowNbr, 1)) > 0 Then
            ws.Rows(RowNbr).PageBreak = xlPageBreakManual
        End If
    Next RowNbr
End Sub

Open in new window

0
 

Author Comment

by:brasiman
ID: 39805161
That worked. You're awesome. Thanks!!!
0
 

Author Closing Comment

by:brasiman
ID: 39805164
Thanks!!
0
 
LVL 21

Expert Comment

by:Ejgil Hedegaard
ID: 39805171
I did not manually add any breaks, it is done by the macro.
You have to run it after each update.
Developers tab, Macros, select the macro.
Or Alt+F11, select Module 1, and run.

Not clear what you mean by wipes out.
The code does not clear any cells.
0
 

Author Comment

by:brasiman
ID: 39805182
I am going to post another questions. When I add more racers, refresh the query, the page break didn't stay above the next race #, it stayed on that same row. So now i have just a few racers on their own page.
0
 

Author Comment

by:brasiman
ID: 39805196
If i don't add the page breaks manually, they don't appear for me...even if i run the macro.
0
 

Author Comment

by:brasiman
ID: 39805206
Is there a way to create a button to run this macro so i don't have to click alt F11 and run the macro every time?
0
 
LVL 21

Expert Comment

by:Ejgil Hedegaard
ID: 39807132
Strange that the reset pagebreak does not work for you.
I changed it to
ws.Cells.PageBreak = xlPageBreakNone

and combined the 2 types Race: and values

Here is a version with a button to start
Example-pagebreaks.xlsm
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
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…
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 …

860 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