macro to insert page breaks

thor10

New Member
Joined
Mar 27, 2002
Messages
14
I need to have a macro that will sort the entire Excel worksheet then insert a page break when the value in lets say column C changes (the next time I use the macro the column could be H. The macro below was given to me from someone in this forum, but it only sorts one column, not the entire worksheet. Would anyone know how to modify this macro or have one that does what I need? All help is greatly appreciated!

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Sub Sort_InsertPageBreaks()
Dim rng As Range, cell As Range
Set rng = Range([B2], [B9999].End(xlUp))
rng.Sort Key1:=rng(1, 1)
For Each cell In rng
If cell.Value <> cell(2, 1).Value Then
cell(2, 1).EntireRow.PageBreak = xlManual
End If
Next
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,214,386
Messages
6,119,214
Members
448,874
Latest member
b1step2far

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