Page Breaks

ronicaroman

New Member
Joined
Dec 2, 2011
Messages
25
I'm trying to creat a macro that will add page breaks for each change in column A, so there should be a page break between RA and DZ in the example below. Is there a simple formula and/or VBA method for doing this?

ColumA...Amount
RA.........25
RA.........25
RA.........75
DZ.........300
DZ.........300
DZ.........55
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You could loop through the contents of column A and add a page break when two consecutive cells differ in value, for example;

Code:
Sub addpagebreaks()
    Dim a1 As Variant, a2 As Variant
    Dim b1 As Integer
    b1 = 1
 
    Do
        a1 = Range("A1").Offset(b1, 0).Value
        a2 = Range("A1").Offset(b1 - 1, 0).Value
        If a1 <> a2 Then
            Worksheets("Sheet1").Rows(b1 + 1).PageBreak = xlPageBreakManual
        End If
        b1 = b1 + 1
    Loop Until a1 = ""
End Sub

Nb. Use xlPageBreakNone if you want to remove the page breaks again.
 
Upvote 0
How would the above differ if I actually have 20 columns (A-T), and I need the page breaks added frome every change in column G?
 
Upvote 0
Please ignore the questions above. Thanks for the code, I was able to tweek it for my 20 column worksheet!

But now I'm having a problem with the first page. Since the column title is different then the value in A2 the code creates a seperate page for row 1. How can I fix this problem. I tried playing with the code a bit to fix this issue but wasn't able to do it. I know I'm missing something obvious.
 
Upvote 0
Change the starting value of b1

Code:
Sub addpagebreaks()
 
    Dim a1 As Variant, a2 As Variant
    Dim b1 As Integer
    [COLOR=red]b1 = 2[/COLOR]
 
    Do
        a1 = Range("A1").Offset(b1, 0).Value
        a2 = Range("A1").Offset(b1 - 1, 0).Value
        If a1 <> a2 Then
            Worksheets("Sheet1").Rows(b1 + 1).PageBreak = xlPageBreakManual
        End If
        b1 = b1 + 1
    Loop Until a1 = ""
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top