Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to add page break as query grows or shrinks

Posted on 2014-01-23
11
Medium Priority
?
273 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
[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
  • 7
  • 4
11 Comments
 
LVL 23

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
Independent Software Vendors: 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!

 
LVL 23

Accepted Solution

by:
Ejgil Hedegaard earned 2000 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 23

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 23

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

715 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