<<Convert Excel Spreadsheets to Web Pages | Trading Software That Operates Within Excel | Convert Excel, Access & Other Databases | Merge Excel Files>>
Ozgrid, Experts in Microsoft Excel Spreadsheets

Excel VBA Macro - Adding Conditional Page Breaks

| | Information Helpful? Why Not Donate.

TRY OUT: Smart-VBA | Code-VBA | Analyzer-XL | Downloader-XL | Trader-XL| More Free Downloads.. Best Value: Finance Templates Bundle

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

DeZaStR needed to write a macro that checks a column of numbers, and each time the number changes, insert a page break. For example, you have a column with all 10's in each row, but on row 25 it then becomes 15's...at that point he wanted a page break.
 
Richie(UK) provided the following code
 
Sub InsertPBs()
Dim rngMyRange As Range, rngCell As Range
 
With Worksheets("Sheet1")
    Set rngMyRange = .Range(.Range("B1"), .Range("B65536").End(xlUp))
    'the range to work with
    For Each rngCell In rngMyRange
    'loop through the range
        If rngCell.Value <> rngCell.Offset(1, 0).Value Then
            .HPageBreaks.Add Before:=rngCell.Offset(1, 0)
        End If
    Next
End With
 
End Sub
 
Epidemic provided an alternative loop

Sub pagebrk()
col = 2 ' what ever column you want to measure.
LastRw = ActiveSheet.UsedRange.Rows.Count
 For x = 2 To LastRw
  If Cells(x, col) <> Cells(x - 1, col) Then
   ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Cells(x, col)
  End If
 Next
End Sub

DeZaStR noted that both coded solutions worked “great” – Thanks guys.

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 [email protected] 31 days after purchase date.


Instant Download and Money Back Guarantee on Most Software

Try out: Analyzer XL | Downloader XL | Smart VBA | Trader XL Pro (best value) | ConsoXL | MergeXL | O2OLAP for Excel | MORE>>

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

GIVE YOURSELF OR YOUR COMPANY 24/7 MICROSOFT EXCEL SUPPORT & QUESTIONS