VBA Setting Page Break to Far Right

bbalch

Board Regular
Joined
Feb 23, 2015
Messages
61
Hello everyone.

I have a workbook with 50 + tabs and I use VBA to loop through and format each tab. Part of the VBA sets the workbook to page break view, drags the page breaks to the far right, and then switches back to normal view. This code takes the dotted blue line that runs vertically between two columns and drags it off to the far right (between column F and G). The code works great until it comes to a tab that does not have a dotted vertical blue line in page break view. I receive a Run-time error '9': Subscript out of range error message when this happens.

Any suggestions on how to: move to the next tab when this error occurs or how to modify the code below to set the page break between columns F and G?

Thanks in advance for your help.
Ben

Code:
    ' Change to page break view - drag off page breaks
    ActiveWindow.View = xlPageBreakPreview
    ActiveSheet.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
        
    ' change to normal view
    ActiveWindow.View = xlNormalView
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hello Ben

- A dotted blue line is an automatic page break.
- If there are no vertical page breaks and the code tries to reference them, an error occurs.
- The examples below show how to delete and move page breaks.

Code:
Sub Delpbreaks()
Dim ws As Worksheet
Set ws = ActiveSheet
ActiveWindow.View = xlPageBreakPreview
If ws.VPageBreaks.Count > 0 Then ws.VPageBreaks(1).DragOff xlToRight, 1
'ActiveWindow.View = xlNormalView
End Sub


Sub Movepbreak()
Dim ws As Worksheet
Set ws = ActiveSheet
ActiveWindow.View = xlPageBreakPreview
Set ws.VPageBreaks(1).Location = [g1]       ' change position
End Sub
 
Upvote 0
That did the trick! Thank you.

The VBA I use is a combination of things I've found and pieced together with bits and pieces of VBA from the macro recorder. When you use the macro recorder and make changes to the page layout you end up with a lot of junk code. I was able to delete and make a change to the page layout code to get the same effect.

Code:
.FitToPagesWide = 1

This code was set to xlAutomatic.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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