Getting existing page breaks in VBA

Haighstrom

New Member
Joined
Aug 30, 2011
Messages
9
I need to write a general function to set a border around each printed page, given a set of page breaks.

The user will set his/her own print areas, and then the macro is required to format a border around these edges (exactly where the blue lines appear in page break preview.

How do I access the page break data in Excel? From here, I imagine it is pretty easy, as I just .Select the pages in turn and apply the relevant formatting, but this first step has me stumped.

Thanks
Haighstrom
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Welcome to MrExcel.

A Worksheet has HPageBreaks and VPageBreaks Collection Objects, each of which has a Range property.
 
Upvote 0
Thanks Andrew. I have written my macro which works almost as intended, but I have a slight bug which is that the formatting of the border is not working. The border is coming out as a dotted orange line... Please advise!

I was hoping that the line "Selection.BorderAround , xlContinuous, xlThick, xlColorIndexAutomatic" would give me a thick black line???

Sub AddPageBorders()
Dim W As Worksheet
Dim Page As Integer
Set W = ActiveSheet
Range(Cells(1, 1), Cells(W.HPageBreaks(1).Location.Row - 1, 9)).Select
Selection.BorderAround , xlContinuous, xlThick, xlColorIndexAutomatic
For Page = 1 To W.HPageBreaks.Count - 1
Range(Cells(W.HPageBreaks(Page).Location.Row, 1), Cells(W.HPageBreaks(Page + 1).Location.Row - 1, 9)).Select
Selection.BorderAround , xlContinuous, xlThick, xlColorIndexAutomatic
Next Page
Range(Cells(W.HPageBreaks(W.HPageBreaks.Count).Location.Row, 1), Cells(Range("A65536").End(xlUp).Row, 9)).Select
Selection.BorderAround , xlContinuous, xlThick, xlColorIndexAutomatic

End Sub
 
Upvote 0
You've got an errant comma in there:

Code:
Selection.BorderAround [COLOR=red][B], [/B][/COLOR]xlContinuous, xlThick, xlColorIndexAutomatic

Better to pass arguments by name.
 
Upvote 0
Thanks. It was throwing errors when I put the arguments in brackets.

All working now, cheers guys.

The working macro was as follows:

Code:
[COLOR=#1f497d][FONT=Calibri]Sub AddPageBorders()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]<o:p> </o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]Dim W As Worksheet<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]Dim Page As Integer<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]<o:p> </o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]Set W = ActiveSheet 'Make typing easier :)<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]<o:p> </o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]'First Page<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]Range(Cells(1, 1), Cells(W.HPageBreaks(1).Location.Row - 1, W.VPageBreaks(1).Location.Column - 1)).Select<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]Selection.BorderAround xlContinuous, xlThin, xlColorIndexAutomatic<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]<o:p> </o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]'Middle Pages<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]For Page = 1 To W.HPageBreaks.Count - 1<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]    Range(Cells(W.HPageBreaks(Page).Location.Row, 1), Cells(W.HPageBreaks(Page + 1).Location.Row - 1, W.VPageBreaks(1).Location.Column - 1)).Select<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]    Selection.BorderAround xlContinuous, xlThin, xlColorIndexAutomatic<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]Next Page<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]<o:p> </o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]'Last Page<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]Range(Cells(W.HPageBreaks(W.HPageBreaks.Count).Location.Row, 1), Cells(Range("A65536").End(xlUp).Row, W.VPageBreaks(1).Location.Column - 1)).Select<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]Selection.BorderAround xlContinuous, xlThin, xlColorIndexAutomatic<o:p></o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]<o:p> </o:p>[/FONT][/COLOR]
[COLOR=#1f497d][FONT=Calibri]End Sub<o:p></o:p>[/FONT][/COLOR]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,617
Messages
6,179,914
Members
452,949
Latest member
beartooth91

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