How to add page break as query grows or shrinks

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
brasimanAsked:
Who is Participating?
 
Ejgil HedegaardConnect With a Mentor Commented:
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
 
Ejgil HedegaardCommented:
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
 
brasimanAuthor Commented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
brasimanAuthor Commented:
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
 
brasimanAuthor Commented:
That worked. You're awesome. Thanks!!!
0
 
brasimanAuthor Commented:
Thanks!!
0
 
Ejgil HedegaardCommented:
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
 
brasimanAuthor Commented:
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
 
brasimanAuthor Commented:
If i don't add the page breaks manually, they don't appear for me...even if i run the macro.
0
 
brasimanAuthor Commented:
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
 
Ejgil HedegaardCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.