Macro to Define Print Area to Two Pages

ttbuson

Board Regular
Joined
Nov 18, 2011
Messages
80
I think I've been staring at this code too long and I can't figure out why it's not working. I wrote this to dynamically adjust the print area to two sheets. The problem I'm encountering is when I have the sheet set up with two print areas already defined and I run this code to adjust it, I get an error in the part of the code that finds and deletes all existing horizontal page breaks. Any thoughts would be appreciated!

Code:
Sub Set_Print_Area_Two_Pages()
ActiveSheet.PageSetup.PrintArea = ""
Dim lastCell, lastCell2 As Integer
lastCell = ActiveSheet.Range("D65536").End(xlUp).Row
lastCell2 = ActiveSheet.Range("S65536").End(xlUp).Row + 1
ActiveSheet.PageSetup.PrintArea = "$C$4:$V$" & lastCell
With ActiveSheet.PageSetup
    .FitToPagesWide = 1
    .FitToPagesTall = 2
End With
ActiveSheet.PageSetup.PrintArea = "$C$4:$V$" & lastCell
Dim x, y As Integer
y = ActiveSheet.HPageBreaks.count
If ActiveSheet.HPageBreaks.count > 0 Then
    For x = 1 To y
        ActiveSheet.HPageBreaks(x).Delete
    Next x
End If
ActiveSheet.HPageBreaks.Add before:=Range("C" & lastCell2)
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,215,226
Messages
6,123,734
Members
449,116
Latest member
alexlomt

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