EXCEL VIDEO TUTORIALS / EXCEL DASHBOARD REPORTS

Ozgrid, Experts in Microsoft Excel Spreadsheets

Excel VBA Macro - Setting Page breaks for variable amounts of data

| | Information Helpful? Why Not Donate.

 

Originally Posted in our Excel Help Forum

Current Special! Complete Excel Excel Training Course for Excel 97 - Excel 2003, only $145.00. $59.95 Instant Buy/Download, 30 Day Money Back Guarantee & Free Excel Help for LIFE!

Got any Excel Questions? Free Excel Help

ChrisBEE1 wrote
 
Hi all,

I trying to develop a macro to print my worksheets. Here's the problem, I never know how much data my end-users will incorporate into any one worksheet. In some instances, one worksheet can result in five pages or more of printout. How do I achieve the following:

1.) set a page break so that if, for example, I have 400 rows or 4000 rows of data that each page print 40 rows data.

2.) Print multiple row column headings on each page.

I would appreciate any help on the subject.
 
Hans Pottel provided a VBA solution
 
Sub PrintAreaWithpageBreaks()
Dim pages As Integer
Dim pageBegin As String
Dim PrArea As String
Dim i As Integer
Dim q As Integer
Dim nRows As Integer, nPagebreaks As Integer
Dim R As Range
Set R = ActiveSheet.UsedRange
'add pagebreak every 40 rows
nRows = R.Rows.Count
If nRows > 40 Then
  nPagebreaks = Int(nRows / 40)
  For i = 1 To nPagebreaks
     ActiveWindow.SelectedSheets.HPageBreaks.Add  Before:=R.Cells(40 * i + 1, 1)
  Next i
End If
'can be used in a separate macro, as I Start counting the number of pagebreaks
pages = ActiveSheet.HPageBreaks.Count
pageBegin = "$A$1"
For i = 1 To pages
  If i > 1 Then pageBegin = ActiveSheet.HPageBreaks(i - 1).Location.Address
  q = ActiveSheet.HPageBreaks(i).Location.Row - 1
  PrArea = pageBegin & ":" & "$H$" & Trim$(Str$(q))
  ActiveSheet.PageSetup.PrintArea = PrArea
  ' the cell in column 1 and in the row immediately below the pagebreak
  ' contains text for the footer
  ActiveSheet.PageSetup.CenterFooter = Cells(q, 1)
'  ActiveSheet.PrintOut copies:=1
Next i
End Sub

Excel Dashboard Reports & Excel Dashboard Charts 50% Off Become an ExcelUser Affiliate & Earn Money

Special! Free Choice of Complete Excel Training Course OR Excel Add-ins Collection on all purchases totaling over $64.00. ALL purchases totaling over $150.00 gets you BOTH! Purchases MUST be made via this site. Send payment proof to special@ozgrid.com 31 days after purchase date.


Instant Download and Money Back Guarantee on Most Software

EXCEL VIDEO TUTORIALS / EXCEL DASHBOARD REPORTS

Excel Trader Package Technical Analysis in Excel With $139.00 of FREE software!

Microsoft ® and Microsoft Excel ® are registered trademarks of Microsoft Corporation. OzGrid is in no way associated with Microsoft

Some of our more popular products are below...
Convert Excel Spreadsheets To Webpages | Trading In Excel | Construction Estimators | Finance Templates & Add-ins Bundle | Code-VBA | Smart-VBA | Print-VBA | Excel Data Manipulation & Analysis | Convert MS Office Applications To...... | Analyzer Excel | Downloader Excel | MSSQL Migration Toolkit | Monte Carlo Add-in | Excel Costing Templates