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