Insert page break "after" condition met?

lmmay

Board Regular
Joined
Jun 6, 2014
Messages
87
I am trying to use the following to print a multiple page worksheet.
Can not figure out what my error is.

VBA Code:
Sub Set_Print_Area()
'Unprotect Worksheet


ActiveSheet.Unprotect Password:="XXXXXXXe"


'Set Print and Review


    Dim c As Range
    Dim FirstAddress As String
   
    Set c = Columns("B").Find(What:="APPLICANT NAME:", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)
    If Not c Is Nothing Then
        FirstAddress = c.Address
        Do
            ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=c.Offset(0)
            Set c = Columns("B").FindNext(c)
        Loop While Not c Is Nothing And c.Address <> FirstAddress
    End If
   
   
   x = ActiveSheet.UsedRange.Columns.Count
   Set lastCell = Cells.SpecialCells(xlCellTypeLastCell)
   ActiveSheet.PageSetup.PrintArea = Range(Cells(1, 1), lastCell).Address
   ActiveSheet.Range("Q2655").End(xlUp).Select
   




ActiveSheet.PrintPreview
 
Last edited by a moderator:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try:
Rich (BB code):
'Replace this line: 
' <!--StartFragment-->
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=c.Offset(0)
'with
Rows(c.Offset(-1).Row).PageBreak = -4135
 
Last edited by a moderator:
Upvote 0
So sorry, I think I had an error in my offset syntax.

Try:
Code:
Rows(c.Row + 1).PageBreak = -4135
 
Upvote 0
Looks to me like your original code should work fine. What happens?

You might want to do a ResetAllPageBreaks first.
 
Upvote 0
I get a "Run Time Error 1004
Application-defined or Objectdefined error on line:
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=c.Offset(0)

At what position should I try a reset?
 
Last edited:
Upvote 0
Try this:

Code:
Sub Set_Print_Area()
  Dim wks           As Worksheet
  Dim cell          As Range
  Dim sAdr          As String

  Set wks = ActiveSheet
  With wks
    .Unprotect Password:="XXXXXXXe"  ' you sure this is correct?

    .ResetAllPageBreaks
    Set cell = Range("B:B").Find(What:="APPLICANT NAME:", LookIn:=xlValues, _
                                 LookAt:=xlWhole, MatchCase:=True)
    If Not cell Is Nothing Then
      sAdr = cell.Address
      Do
        .HPageBreaks.Add Before:=cell
        Set cell = Range("B:B").FindNext(cell)
      Loop While cell.Address <> sAdr
    End If

    .PrintPreview
  End With
End Sub
 
Upvote 0
Have you verified that the sheet password is correct?
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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