Solved

How to add page break as query grows or shrinks

Posted on 2014-01-23
11
242 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro 6 49
Match formula returns N/A 5 25
Excel Formula 4 28
What is format f12.8 for a CSV file 6 37
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
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 will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

919 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

14 Experts available now in Live!

Get 1:1 Help Now