Solved

How to add page break as query grows or shrinks

Posted on 2014-01-23
11
230 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 21

Accepted Solution

by:
Ejgil Hedegaard earned 500 total points
Comment Utility
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
Comment Utility
That worked. You're awesome. Thanks!!!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Closing Comment

by:brasiman
Comment Utility
Thanks!!
0
 
LVL 21

Expert Comment

by:Ejgil Hedegaard
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now