VBA to add page breaks on varible data

DeanRobinson

New Member
Joined
Sep 1, 2011
Messages
35
Hi all, id like to find a way of adding a page break each time a cell value changes but not taking into account blank cells. so for example

AW
AW
AW
SPACE
AW
AW
AW
SPACE
PAGE BREAK
GS
GS
GS
GS
SPACE

So the page break is because the cell has changed from AW to GS, but there were spaces in AW's range that i dont want to take into account if that makes sense.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi,
Try this.
Code:
Sub AddPageBreaksAtChanges()
    Dim i As Long
    Dim lr As Long
    Dim strLastVal As String
    lr = Range("A" & Rows.Count).End(xlUp).Row
    'change the second 1 in the cells() references to whatever
    'column your values are in
    For i = 1 To lr
        If Trim(Cells(i, 1)) <> "" Then
            If strLastVal <> "" Then
                If Cells(i, 1) <> strLastVal Then
                    strLastVal = Cells(i, 1)
                    ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Cells(i, 1)
                End If
            Else
                strLastVal = Cells(i, 1)
            End If
        End If
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

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