Friday 7 June 2013

How can I easily toggle display of page breaks for all worksheets in an Excel 2010 workbook?

How can I easily toggle display of page breaks for all worksheets in an Excel 2010 workbook?

Excel 2010 only allows turning page breaks off or on for one worksheet at a time via File → Options → Advanced → "Display options for this Worksheet":

I previously came up with a VBA macro to toggle page breaks, but it only works on the sheet I have active:
Sub TogglePageBreaks()

    ActiveSheet.DisplayPageBreaks = Not ActiveSheet.DisplayPageBreaks

End Sub
The next logical question (which someone else had to point out to me) is how do I use a macro to toggle page breaks display for all worksheets in the active workbook?
Being new to VBA, I spent a couple hours researching how to loop through worksheets and also how the DisplayPageBreaks object works. I came up with an answer below.

No comments:

Post a Comment